Skip to content

Commit c5d2741

Browse files
committed
Added initial support for const qualifier
1 parent 3227164 commit c5d2741

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

cpp2util.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -196,33 +196,33 @@ class out {
196196
#define CPP2_SOURCE_LOCATION_PARAM , std::source_location where
197197
#define CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT , std::source_location where = std::source_location::current()
198198
#define CPP2_SOURCE_LOCATION_PARAM_SOLO std::source_location where
199-
//#define CPP2_SOURCE_LOCATION_PARAM_SOLO_ANON std::source_location
200199
#define CPP2_SOURCE_LOCATION_ARG , where
201-
//#define CPP2_SOURCE_LOCATION_ARG_SOLO where
202200
#else
203201
#define CPP2_SOURCE_LOCATION_PARAM
204202
#define CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT
205203
#define CPP2_SOURCE_LOCATION_PARAM_SOLO
206-
//#define CPP2_SOURCE_LOCATION_PARAM_SOLO_ANON
207204
#define CPP2_SOURCE_LOCATION_ARG
208-
//#define CPP2_SOURCE_LOCATION_ARG_SOLO
209205
#endif
210206

207+
// For C++23: make this std::string_view and drop the macro
208+
// Before C++23 std::string_view was not guaranteed to be trivially copyable,
209+
// and so in<T> will pass it by const& and really it should be by value
210+
#define CPP2_MESSAGE_PARAM char const*
211211

212212
class contract_group {
213213
public:
214-
using handler = void (*)(const char* msg CPP2_SOURCE_LOCATION_PARAM) noexcept;
214+
using handler = void (*)(CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM) noexcept;
215215

216216
constexpr contract_group (handler h = nullptr) : reporter(h) { }
217217
constexpr auto set_handler(handler h) -> handler { assert(h); auto old = reporter; reporter = h; return old; }
218218
constexpr auto get_handler() const -> handler { return reporter; }
219-
constexpr auto expects (bool b, const char* msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT)
219+
constexpr auto expects (bool b, CPP2_MESSAGE_PARAM msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT)
220220
-> void { if (!b) reporter(msg CPP2_SOURCE_LOCATION_ARG); }
221221
private:
222222
handler reporter;
223223
};
224224

225-
[[noreturn]] auto report_and_terminate(std::string_view group, const char* msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) noexcept -> void {
225+
[[noreturn]] auto report_and_terminate(std::string_view group, CPP2_MESSAGE_PARAM msg = "" CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAULT) noexcept -> void {
226226
std::cerr
227227
#ifdef CPP2_USE_SOURCE_LOCATION
228228
<< where.file_name() << "2("
@@ -238,27 +238,27 @@ class contract_group {
238238
}
239239

240240
auto inline Default = contract_group(
241-
[](const char* msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
241+
[](CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
242242
report_and_terminate("Contract", msg CPP2_SOURCE_LOCATION_ARG);
243243
}
244244
);
245245
auto inline Bounds = contract_group(
246-
[](const char* msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
246+
[](CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
247247
report_and_terminate("Bounds safety", msg CPP2_SOURCE_LOCATION_ARG);
248248
}
249249
);
250250
auto inline Null = contract_group(
251-
[](const char* msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
251+
[](CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
252252
report_and_terminate("Null safety", msg CPP2_SOURCE_LOCATION_ARG);
253253
}
254254
);
255255
auto inline Type = contract_group(
256-
[](const char* msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
256+
[](CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
257257
report_and_terminate("Type safety", msg CPP2_SOURCE_LOCATION_ARG);
258258
}
259259
);
260260
auto inline Testing = contract_group(
261-
[](const char* msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
261+
[](CPP2_MESSAGE_PARAM msg CPP2_SOURCE_LOCATION_PARAM)noexcept {
262262
report_and_terminate("Testing", msg CPP2_SOURCE_LOCATION_ARG);
263263
}
264264
);

cppfront.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,11 @@ class cppfront
845845
assert(n.identifier);
846846
emit(*n.identifier);
847847

848+
if (n.const_qualifier) {
849+
printer.print_cpp2(" ", n.const_qualifier->position());
850+
emit(*n.const_qualifier);
851+
}
852+
848853
if (!n.template_args.empty()) {
849854
printer.print_cpp2("<", n.open_angle);
850855
auto first = true;
@@ -1559,6 +1564,10 @@ class cppfront
15591564
}
15601565
else {
15611566
emit( id_expr );
1567+
if (n.declaration->pointer_declarator) {
1568+
printer.print_cpp2(" ", n.declaration->pointer_declarator->position());
1569+
emit(*n.declaration->pointer_declarator);
1570+
}
15621571
}
15631572

15641573
// Then any suffix
@@ -1682,8 +1691,12 @@ class cppfront
16821691
assert(n.condition);
16831692
emit (*n.condition);
16841693
printer.print_cpp2(", ", n.position());
1685-
assert (n.message);
1686-
emit (*n.message);
1694+
if (n.message) {
1695+
emit (*n.message);
1696+
}
1697+
else {
1698+
printer.print_cpp2("\"\"", n.position());
1699+
}
16871700
printer.print_cpp2(");", n.position());
16881701
}
16891702

@@ -1861,7 +1874,7 @@ class cppfront
18611874
function_returns.pop_back();
18621875
}
18631876

1864-
// Object with initializer
1877+
// Object with optional initializer
18651878
else if (!printer.doing_declarations_only() && n.is(declaration_node::object))
18661879
{
18671880
auto& type = std::get<declaration_node::object>(n.type);

parse.h

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ auto prefix_expression_node::visit(auto& v, int depth) -> void
332332

333333
struct unqualified_id_node
334334
{
335-
token const* identifier;
335+
token const* const_qualifier = {}; // optional
336+
token const* identifier = {}; // required
336337

337338
enum active { empty=0, expression, id_expression };
338339

@@ -349,11 +350,6 @@ struct unqualified_id_node
349350
};
350351
std::vector<term> template_args;
351352

352-
unqualified_id_node( token const* tok)
353-
: identifier{ tok }
354-
{
355-
}
356-
357353
auto position() const -> source_position
358354
{
359355
assert (identifier);
@@ -363,6 +359,9 @@ struct unqualified_id_node
363359
auto visit(auto& v, int depth) -> void
364360
{
365361
v.start(*this, depth);
362+
if (const_qualifier) {
363+
v.start(*const_qualifier, depth+1);
364+
}
366365
assert (identifier);
367366
v.start(*identifier, depth+1);
368367

@@ -1481,8 +1480,8 @@ class parser
14811480

14821481

14831482
//G unqualified-id:
1484-
//G identifier
1485-
//G template-id
1483+
//G const-opt identifier
1484+
//G const-opt template-id
14861485
//GT operator-function-id
14871486
//G
14881487
//G template-id:
@@ -1499,11 +1498,19 @@ class parser
14991498
{
15001499
// Handle the identifier
15011500
if (curr().type() != lexeme::Identifier &&
1502-
curr().type() != lexeme::Keyword) // because of fundamental types like 'int' which are keywords
1501+
curr().type() != lexeme::Keyword) // 'const', and fundamental types that are keywords
15031502
{
15041503
return {};
15051504
}
1506-
auto n = std::make_unique<unqualified_id_node>( &curr() );
1505+
1506+
auto n = std::make_unique<unqualified_id_node>();
1507+
1508+
if (curr().type() == lexeme::Keyword && curr() == "const") {
1509+
n->const_qualifier = &curr();
1510+
next();
1511+
}
1512+
1513+
n->identifier = &curr();
15071514
next();
15081515

15091516
// Handle the template-argument-list if there is one

0 commit comments

Comments
 (0)