Discussion:
Do contextual name scopes exist?
Add Reply
Rick C. Hodgin
2017-05-15 18:48:18 UTC
Reply
Permalink
Raw Message
Does anyone know if such a thing as contextual name scopes exist,
where the same named token can be used, but it's identified not
just by its name and natural scope, but also its usage scope?

For example, in the following code, it's clear by syntax what the
targets are even though they use the same token name:

void func(void** p)
{
// Assume void** p is valid
void *p = *p;

*p = p;
}

I'm wondering if such a thing as this type of contextual name scopes
exist in any flavor of C? The above would be interpreted as:

void func(void** p1)
{
// Assume void** p1 is valid
void *p2 = *p1;

*p1 = p2;
}

Thank you,
Rick C. Hodgin
John Bode
2017-05-15 19:52:36 UTC
Reply
Permalink
Raw Message
Post by Rick C. Hodgin
Does anyone know if such a thing as contextual name scopes exist,
where the same named token can be used, but it's identified not
just by its name and natural scope, but also its usage scope?
For example, in the following code, it's clear by syntax what the
void func(void** p)
{
// Assume void** p is valid
void *p = *p;
*p = p;
}
I'm wondering if such a thing as this type of contextual name scopes
exist in any flavor of C?
No such "usage scope" is defined by the language standard, and I can't think
of any implementation I've used over the years that would have allowed that
code to compile and work as expected.
Post by Rick C. Hodgin
...If an identifier designates two different entities in the same name
space, the scopes might overlap. If so, the scope of one entity (the
inner scope) will end strictly before the scope of the other entity (the
outer scope). Within the inner scope, the identifier designates the entity
declared in the inner scope; the entity declared in the outer scope is
hidden (and not visible) within the inner scope.
So code like the above is not allowed, since the scope of the parameter "p"
does not end strictly before the scope of the local object "p" (since you
are trying to use it as an initializer).
m***@gmail.com
2017-05-16 07:51:12 UTC
Reply
Permalink
Raw Message
Post by Rick C. Hodgin
Does anyone know if such a thing as contextual name scopes exist,
where the same named token can be used, but it's identified not
just by its name and natural scope, but also its usage scope?
For example, in the following code, it's clear by syntax what the
void func(void** p)
{
// Assume void** p is valid
void *p = *p;
*p = p;
}
I'm wondering if such a thing as this type of contextual name scopes
If you want to obfuscate your code, there's plenty of existing ways to do it.
bartc
2017-05-16 08:46:04 UTC
Reply
Permalink
Raw Message
Post by Rick C. Hodgin
Does anyone know if such a thing as contextual name scopes exist,
where the same named token can be used, but it's identified not
just by its name and natural scope, but also its usage scope?
For example, in the following code, it's clear by syntax what the
void func(void** p)
{
// Assume void** p is valid
void *p = *p;
*p = p;
}
I'm wondering if such a thing as this type of contextual name scopes
void func(void** p1)
{
// Assume void** p1 is valid
void *p2 = *p1;
*p1 = p2;
}
There would be too much ambiguity I think. *p=p would match a (void**,
void*) as in your example, but also a (void***, void**). And since void*
is involved, *p would also match a char*, int*, float* etc:

{float* p; {int* p; {char* p; {void* p = p; // ??

And what happens here:

{void* p; {void* p; {void *p; p=p; ... // ??
--
bartc
Loading...