Skip to content

Commit ced84b7

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

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

source/cppfront.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,10 @@ class cppfront
10711071
assert(n.identifier);
10721072
emit(*n.identifier);
10731073

1074+
for (auto _ : n.pointer_declarators) {
1075+
printer.print_cpp2("*", n.position());
1076+
}
1077+
10741078
if (!n.template_args.empty()) {
10751079
printer.print_cpp2("<", n.open_angle);
10761080
auto first = true;
@@ -1572,9 +1576,9 @@ class cppfront
15721576
auto& unqual = std::get<id_expression_node::unqualified>(id->id);
15731577
assert(unqual);
15741578
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) {
1579+
// TODO: Generalize this -- for now we detect only multi-level cases of the form "p: ***int = ...;"
1580+
// We don't recognize pointer types that are deduced or from Cpp1
1581+
if (decl && decl->declaration && !decl->declaration->pointer_declarators.empty()) {
15781582
if (n.ops.empty()) {
15791583
last_postfix_expr_was_pointer = true;
15801584
}
@@ -2155,10 +2159,6 @@ class cppfront
21552159
}
21562160
else {
21572161
emit( id_expr );
2158-
if (n.declaration->pointer_declarator) {
2159-
printer.print_cpp2(" ", n.declaration->pointer_declarator->position());
2160-
emit(*n.declaration->pointer_declarator);
2161-
}
21622162
}
21632163

21642164
// Then any suffix
@@ -2538,9 +2538,6 @@ class cppfront
25382538
}
25392539
printer.preempt_position(n.position());
25402540
emit( *type );
2541-
if (n.pointer_declarator) {
2542-
printer.print_cpp2("*", n.position());
2543-
}
25442541
// one pointer is enough for now, pointer-to-function fun can be later
25452542
if (!n.initializer) {
25462543
printer.print_cpp2( ">", n.position() );

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)