Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The concept of a type name scope in C11

ISO C11 Standard (I only have access to a draft version, n1570.pdf) states in 6.2.1.8 (there is no such paragraph in C99 standard):

As a special case, a type name (which is not a declaration of an identifier) is considered to have a scope that begins just after the place within the type name where the omitted identifier would appear were it not omitted.

I am looking for any explanation of the following:

1) The name of the section (6.2.1) is "Scopes of identifiers". The wording of this paragraph is unclear to me: is a 'type name scope' a kind of scope, similar to, e.g. block scope, file scope, etc? Or is it the scope of the type name itself (in which case a further question is how can an object without a name have a scope and what is a possible use for such scope).

2) Where does this definition matter? More specifically, if it were changed to say that the scope of a type name starts immediately after the corresponding type name is complete, what would that affect? The only tokens that can follow the omitted identifier in the abstract declarator(=type name) are a few parentheses and a list of parameter names or array dimensions (with expressions inside) neither of which can refer to the type name in question since there is no identifier to reference. Why not wait till the declarator is complete?

like image 231
alexsh Avatar asked Mar 10 '14 23:03

alexsh


People also ask

What is scope of an identifier in C++?

In this article The scope of an identifier is the part of the program in which the name can be used. This is sometimes called "lexical scope." There are four kinds of scope: function, file, block, and function prototype.

What is function prototype scope in C?

In a function declaration (also called a function prototype) or in any function declarator—except the declarator of a function definition—parameter names have function prototype scope. Function prototype scope terminates at the end of the nearest enclosing function declarator.

How we can use scope identifier?

The scope of an identifier begins at its declaration. 3. If the declaration is within a compound statement, the scope of the identifier ends at the end of that compound statement. We say the identifier is local to that compound statement (or block).


1 Answers

This wording is the subject of Defect Report #341: [*] in abstract declarators, which discuses the problem that in the C99 standard type-name has no scope but scope is required in certain instances and so C99 required a fix. The defect report says (emphasis mine):

6.7.5.2#4 says that * as an array size "can only be used in declarations with function prototype scope", and paragraph 5 says "If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *".

But is a type name in a function prototype a declaration, and does it have function prototype scope? Scopes are only defined in 6.2.1 for identifiers, and such type names do not declare identifiers. The presence of [*] in the syntax for abstract declarators suggests that

void f(int (*)[*]);

was intended to be valid and void f(int (*)[a]); was intended to be equivalent to it, but there are no declarations at function prototype scope involved. [...]

the current wording is the resolution of this issue the comments include the folowing:

It appears the issue hinges entirely on the point that a type-name is not a declaration and does not declare an identifier, and because of that it has no scope. Instead of adding complex wording to avoid using the term "scope" as suggested in the DR, it seems clearer to modify the definition of Scope such that it applies to type-name, which is described in 6.7.6 as "syntactically a declaration for a function or an object of that type that omits the identifier".

this is also effects the wording in section 6.7.5.2 paragraph 4 which changes the phrase from:

[...]declarations with function prototype scope[...]

to:

[...]declarations or type-names with function prototype scope[...]

like image 113
Shafik Yaghmour Avatar answered Oct 06 '22 23:10

Shafik Yaghmour