Skip to content

Commit 0672e85

Browse files
committed
Add and use cpp_token_string for unexposed entities
1 parent e53fc5e commit 0672e85

29 files changed

+799
-491
lines changed

include/cppast/code_generator.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,12 +500,17 @@ namespace cppast
500500
/// \exclude
501501
class cpp_template_argument;
502502

503+
/// \exclude
504+
class cpp_token_string;
505+
503506
/// \exclude
504507
namespace detail
505508
{
506509
void write_template_arguments(
507510
code_generator::output& output,
508511
type_safe::optional<type_safe::array_ref<const cpp_template_argument>> arguments);
512+
513+
void write_token_string(code_generator::output& output, const cpp_token_string& tokens);
509514
} // namespace detail
510515
} // namespace cppast
511516

include/cppast/cpp_entity.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <type_safe/optional_ref.hpp>
1212

1313
#include <cppast/detail/intrusive_list.hpp>
14+
#include <cppast/cpp_token.hpp>
1415

1516
namespace cppast
1617
{
@@ -141,9 +142,7 @@ namespace cppast
141142

142143
protected:
143144
/// \effects Creates it giving it the the name.
144-
cpp_entity(std::string name) : name_(std::move(name)), user_data_(nullptr)
145-
{
146-
}
145+
cpp_entity(std::string name) : name_(std::move(name)), user_data_(nullptr) {}
147146

148147
private:
149148
/// \returns The kind of the entity.
@@ -182,27 +181,27 @@ namespace cppast
182181
/// \returns A newly built and registered unexposed entity.
183182
/// \notes It will be registered as a declaration.
184183
static std::unique_ptr<cpp_entity> build(const cpp_entity_index& index, cpp_entity_id id,
185-
std::string name, std::string spelling);
184+
std::string name, cpp_token_string spelling);
186185

187186
/// \returns A newly built unnamed unexposed entity.
188187
/// It will not be registered.
189-
static std::unique_ptr<cpp_entity> build(std::string spelling);
188+
static std::unique_ptr<cpp_entity> build(cpp_token_string spelling);
190189

191190
/// \returns The spelling of that entity.
192-
const std::string& spelling() const noexcept
191+
const cpp_token_string& spelling() const noexcept
193192
{
194193
return spelling_;
195194
}
196195

197196
private:
198-
cpp_unexposed_entity(std::string name, std::string spelling)
197+
cpp_unexposed_entity(std::string name, cpp_token_string spelling)
199198
: cpp_entity(std::move(name)), spelling_(std::move(spelling))
200199
{
201200
}
202201

203202
cpp_entity_kind do_get_entity_kind() const noexcept override;
204203

205-
std::string spelling_;
204+
cpp_token_string spelling_;
206205
};
207206

208207
/// \returns Whether or not the entity is templated.

include/cppast/cpp_expression.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <atomic>
99
#include <memory>
1010

11+
#include <cppast/cpp_token.hpp>
1112
#include <cppast/cpp_type.hpp>
1213

1314
namespace cppast
@@ -82,20 +83,20 @@ namespace cppast
8283
public:
8384
/// \returns A newly created unexposed expression.
8485
static std::unique_ptr<cpp_unexposed_expression> build(std::unique_ptr<cpp_type> type,
85-
std::string str)
86+
cpp_token_string str)
8687
{
8788
return std::unique_ptr<cpp_unexposed_expression>(
8889
new cpp_unexposed_expression(std::move(type), std::move(str)));
8990
}
9091

9192
/// \returns The expression as a string.
92-
const std::string& expression() const noexcept
93+
const cpp_token_string& expression() const noexcept
9394
{
9495
return str_;
9596
}
9697

9798
private:
98-
cpp_unexposed_expression(std::unique_ptr<cpp_type> type, std::string str)
99+
cpp_unexposed_expression(std::unique_ptr<cpp_type> type, cpp_token_string str)
99100
: cpp_expression(std::move(type)), str_(std::move(str))
100101
{
101102
}
@@ -105,7 +106,7 @@ namespace cppast
105106
return cpp_expression_kind::unexposed_t;
106107
}
107108

108-
std::string str_;
109+
cpp_token_string str_;
109110
};
110111

111112
/// A [cppast::cpp_expression]() that is a literal.

include/cppast/cpp_template.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <cppast/cpp_entity.hpp>
1313
#include <cppast/cpp_entity_container.hpp>
14+
#include <cppast/cpp_token.hpp>
1415
#include <cppast/cpp_template_parameter.hpp>
1516

1617
namespace cppast
@@ -179,7 +180,7 @@ namespace cppast
179180
}
180181

181182
type_safe::variant<std::vector<cpp_template_argument>, std::string> arguments_;
182-
cpp_template_ref templ_;
183+
cpp_template_ref templ_;
183184
};
184185

185186
/// Base class for all entities modelling a C++ template specialization.
@@ -214,9 +215,9 @@ namespace cppast
214215
/// \requires The arguments are not exposed, i.e. `arguments_exposed()` returns `false`.
215216
/// \notes For function template specializations it can be empty,
216217
/// meaning that the arguments are not explictly given but deduced from the signature.
217-
const std::string& unexposed_arguments() const noexcept
218+
const cpp_token_string& unexposed_arguments() const noexcept
218219
{
219-
return arguments_.value(type_safe::variant_type<std::string>{});
220+
return arguments_.value(type_safe::variant_type<cpp_token_string>{});
220221
}
221222

222223
/// \returns Whether or not the specialization is a full specialization.
@@ -252,7 +253,7 @@ namespace cppast
252253
}
253254

254255
/// \effects Adds unexposed arguments as string.
255-
void add_unexposed_arguments(std::string arg)
256+
void add_unexposed_arguments(cpp_token_string arg)
256257
{
257258
auto& specialization =
258259
static_cast<cpp_template_specialization&>(*this->template_entity);
@@ -276,8 +277,8 @@ namespace cppast
276277
}
277278

278279
private:
279-
type_safe::variant<std::vector<cpp_template_argument>, std::string> arguments_;
280-
cpp_entity_id templ_;
280+
type_safe::variant<std::vector<cpp_template_argument>, cpp_token_string> arguments_;
281+
cpp_entity_id templ_;
281282
};
282283
} // namespace cppast
283284

include/cppast/cpp_token.hpp

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
2+
// This file is subject to the license terms in the LICENSE file
3+
// found in the top-level directory of this distribution.
4+
5+
#ifndef CPPAST_CPP_TOKEN_HPP_INCLUDED
6+
#define CPPAST_CPP_TOKEN_HPP_INCLUDED
7+
8+
#include <string>
9+
#include <vector>
10+
11+
#include <type_safe/reference.hpp>
12+
13+
namespace cppast
14+
{
15+
/// The kinds of C++ tokens.
16+
enum class cpp_token_kind
17+
{
18+
identifier, //< Any identifier.
19+
keyword, //< Any keyword.
20+
literal, //< Any literal.
21+
punctuation, //< Any other punctuation.
22+
23+
unknown, //< An unknown token.
24+
};
25+
26+
/// A C++ token.
27+
struct cpp_token
28+
{
29+
std::string spelling;
30+
cpp_token_kind kind;
31+
32+
cpp_token(cpp_token_kind kind, std::string spelling)
33+
: spelling(std::move(spelling)), kind(kind)
34+
{
35+
}
36+
37+
friend bool operator==(const cpp_token& lhs, const cpp_token& rhs) noexcept
38+
{
39+
return lhs.spelling == rhs.spelling;
40+
}
41+
42+
friend bool operator!=(const cpp_token& lhs, const cpp_token& rhs) noexcept
43+
{
44+
return !(rhs == lhs);
45+
}
46+
};
47+
48+
/// A combination of multiple C++ tokens.
49+
class cpp_token_string
50+
{
51+
public:
52+
/// Builds a token string.
53+
class builder
54+
{
55+
public:
56+
builder() = default;
57+
58+
/// \effects Adds a token.
59+
void add_token(cpp_token tok)
60+
{
61+
tokens_.push_back(std::move(tok));
62+
}
63+
64+
/// \effects Converts a trailing `>>` to `>` token.
65+
void unmunch();
66+
67+
/// \returns The finished string.
68+
cpp_token_string finish()
69+
{
70+
return cpp_token_string(std::move(tokens_));
71+
}
72+
73+
private:
74+
std::vector<cpp_token> tokens_;
75+
};
76+
77+
/// \effects Creates it from a sequence of tokens.
78+
cpp_token_string(std::vector<cpp_token> tokens) : tokens_(std::move(tokens)) {}
79+
80+
/// \effects Creates from a string.
81+
/// \notes This does not do tokenization, it will only store a single, unknown token!
82+
static cpp_token_string from_string(std::string str)
83+
{
84+
return cpp_token_string({cpp_token(cpp_token_kind::unknown, std::move(str))});
85+
}
86+
87+
/// \exclude target
88+
using iterator = std::vector<cpp_token>::const_iterator;
89+
90+
/// \returns An iterator to the first token.
91+
iterator begin() const noexcept
92+
{
93+
return tokens_.begin();
94+
}
95+
96+
/// \returns An iterator one past the last token.
97+
iterator end() const noexcept
98+
{
99+
return tokens_.end();
100+
}
101+
102+
/// \returns Whether or not the string is empty.
103+
bool empty() const noexcept
104+
{
105+
return tokens_.empty();
106+
}
107+
108+
/// \returns A reference to the first token.
109+
const cpp_token& front() const noexcept
110+
{
111+
return tokens_.front();
112+
}
113+
114+
/// \returns A reference to the last token.
115+
const cpp_token& back() const noexcept
116+
{
117+
return tokens_.back();
118+
}
119+
120+
/// \returns The string representation of the tokens, without any whitespace.
121+
std::string as_string() const;
122+
123+
private:
124+
std::vector<cpp_token> tokens_;
125+
126+
friend bool operator==(const cpp_token_string& lhs, const cpp_token_string& rhs);
127+
};
128+
129+
bool operator==(const cpp_token_string& lhs, const cpp_token_string& rhs);
130+
131+
inline bool operator!=(const cpp_token_string& lhs, const cpp_token_string& rhs)
132+
{
133+
return !(lhs == rhs);
134+
}
135+
} // namespace cppast
136+
137+
#endif // CPPAST_CPP_TOKEN_HPP_INCLUDED

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ set(header
3636
../include/cppast/cpp_storage_class_specifiers.hpp
3737
../include/cppast/cpp_template.hpp
3838
../include/cppast/cpp_template_parameter.hpp
39+
../include/cppast/cpp_token.hpp
3940
../include/cppast/cpp_type.hpp
4041
../include/cppast/cpp_type_alias.hpp
4142
../include/cppast/cpp_variable.hpp
@@ -68,6 +69,7 @@ set(source
6869
cpp_preprocessor.cpp
6970
cpp_static_assert.cpp
7071
cpp_template_parameter.cpp
72+
cpp_token.cpp
7173
cpp_type.cpp
7274
cpp_type_alias.cpp
7375
cpp_variable.cpp

0 commit comments

Comments
 (0)