Skip to content

Commit 5355204

Browse files
committed
Change get-decl-of to use reverse iters, closes #102
Renamed `get_declaration_of` to `get_local_declaration_of` to emphasize that this one is intra-function (we'll eventually want a non-local version and that future one should have the non-`local` name)
1 parent 30ec697 commit 5355204

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

source/cppfront.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ class cppfront
10901090
printer.print_cpp2(".value()", n.position());
10911091
}
10921092
else if (!in_definite_init && !in_parameter_list) {
1093-
if (auto decl = sema.get_declaration_of(*n.identifier);
1093+
if (auto decl = sema.get_local_declaration_of(*n.identifier);
10941094
is_local_name &&
10951095
decl &&
10961096
// note pointer equality: if we're not in the actual declaration of n.identifier
@@ -1571,7 +1571,7 @@ class cppfront
15711571
{
15721572
auto& unqual = std::get<id_expression_node::unqualified>(id->id);
15731573
assert(unqual);
1574-
auto decl = sema.get_declaration_of(*unqual->identifier);
1574+
auto decl = sema.get_local_declaration_of(*unqual->identifier);
15751575
// TODO: Generalize this -- for now we detect only cases of the form "p: *int = ...;"
15761576
// We don't recognize pointer types that are deduced, multi-level, or from Cpp1
15771577
if (decl && decl->declaration && decl->declaration->pointer_declarator) {

source/sema.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ class sema
226226
{
227227
}
228228

229-
230-
auto get_declaration_of(token const& t) -> declaration_sym const*
229+
// Get the declaration of t within the same function
230+
//
231+
auto get_local_declaration_of(token const& t) -> declaration_sym const*
231232
{
232233
// First find the position the query is coming from
233234
// and remember its depth
@@ -244,11 +245,12 @@ class sema
244245

245246
// Then look backward to find the first declaration of
246247
// this name that is not deeper (in a nested scope)
247-
for ( ; i != symbols.cbegin(); --i )
248+
// and is in the same function
249+
for (auto ri = std::make_reverse_iterator(i+1); ri != symbols.crend(); ++ri )
248250
{
249-
if (i->sym.index() == symbol::active::declaration && i->depth <= depth)
251+
if (ri->sym.index() == symbol::active::declaration && ri->depth <= depth)
250252
{
251-
auto const& decl = std::get<symbol::active::declaration>(i->sym);
253+
auto const& decl = std::get<symbol::active::declaration>(ri->sym);
252254

253255
// Don't look beyond the current function
254256
assert(decl.declaration);
@@ -260,7 +262,7 @@ class sema
260262
if (decl.identifier && *decl.identifier == t) {
261263
return &decl;
262264
}
263-
depth = i->depth;
265+
depth = ri->depth;
264266
}
265267
}
266268

@@ -890,7 +892,7 @@ class sema
890892
{
891893
// Put this into the table if it's a use of an object in scope
892894
// or it's a 'copy' parameter
893-
if (auto decl = get_declaration_of(t);
895+
if (auto decl = get_local_declaration_of(t);
894896
decl
895897
)
896898
{

0 commit comments

Comments
 (0)