Skip to content

Commit 9fe1be7

Browse files
committed
Add support of multi-level pointers
1 parent b1754db commit 9fe1be7

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

source/cppfront.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,9 +1572,9 @@ class cppfront
15721572
auto& unqual = std::get<id_expression_node::unqualified>(id->id);
15731573
assert(unqual);
15741574
auto decl = sema.get_declaration_of(*unqual->identifier);
1575-
// TODO: Generalize this -- for now we detect only cases of the form "p: *int = ...;"
1576-
// We don't recognize pointer types that are deduced, multi-level, or from Cpp1
1577-
if (decl && decl->declaration && decl->declaration->pointer_declarator) {
1575+
// TODO: Generalize this -- for now we detect only multi-level cases of the form "p: ***int = ...;"
1576+
// We don't recognize pointer types that are deduced or from Cpp1
1577+
if (decl && decl->declaration && !decl->declaration->pointer_declarators.empty()) {
15781578
if (n.ops.empty()) {
15791579
last_postfix_expr_was_pointer = true;
15801580
}
@@ -2155,9 +2155,9 @@ class cppfront
21552155
}
21562156
else {
21572157
emit( id_expr );
2158-
if (n.declaration->pointer_declarator) {
2159-
printer.print_cpp2(" ", n.declaration->pointer_declarator->position());
2160-
emit(*n.declaration->pointer_declarator);
2158+
for (const auto pointer_declarator : n.declaration->pointer_declarators) {
2159+
printer.print_cpp2(" ", pointer_declarator->position());
2160+
emit(*pointer_declarator);
21612161
}
21622162
}
21632163

@@ -2538,7 +2538,7 @@ class cppfront
25382538
}
25392539
printer.preempt_position(n.position());
25402540
emit( *type );
2541-
if (n.pointer_declarator) {
2541+
for (auto _ : n.pointer_declarators) {
25422542
printer.print_cpp2("*", n.position());
25432543
}
25442544
// one pointer is enough for now, pointer-to-function fun can be later

source/parse.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ struct declaration_node
846846
source_position pos;
847847
std::unique_ptr<unqualified_id_node> identifier;
848848

849-
token const* pointer_declarator = nullptr;
849+
std::vector<token const*> pointer_declarators;
850850

851851
enum active { function, object };
852852
std::variant<
@@ -2812,8 +2812,10 @@ class parser
28122812

28132813
// Or a pointer to a type, declaring a pointer object
28142814
else if (curr().type() == lexeme::Multiply) {
2815-
n->pointer_declarator = &curr();
2816-
next();
2815+
while (curr().type() == lexeme::Multiply) {
2816+
n->pointer_declarators.push_back(&curr());
2817+
next();
2818+
}
28172819
if (auto t = id_expression()) {
28182820
n->type = std::move(t);
28192821
assert (n->type.index() == declaration_node::object);
@@ -2866,7 +2868,7 @@ class parser
28662868
n->equal_sign = curr().position();
28672869
next();
28682870

2869-
if (n->pointer_declarator) {
2871+
if (!n->pointer_declarators.empty()) {
28702872
if (curr() == "nullptr" ||
28712873
isdigit(std::string_view(curr())[0]) ||
28722874
(curr() == "(" && peek(1) && *peek(1) == ")")

0 commit comments

Comments
 (0)