Language Specification Version 0 Notice



Download 3.2 Mb.
Page29/85
Date29.01.2017
Size3.2 Mb.
#10878
1   ...   25   26   27   28   29   30   31   32   ...   85

7.5Primary expressions


Primary expressions include the simplest forms of expressions.

primary-expression:
primary-no-array-creation-expression
array-creation-expression


primary-no-array-creation-expression:
literal
simple-name
parenthesized-expression
member-access
invocation-expression
element-access
this-access
base-access
post-increment-expression
post-decrement-expression
object-creation-expression
delegate-creation-expression
anonymous-object-creation-expression
typeof-expression
checked-expression
unchecked-expression
default-value-expression
anonymous-method-expression

Primary expressions are divided between array-creation-expressions and primary-no-array-creation-expressions. Treating array-creation-expression in this way, rather than listing it along with the other simple expression forms, enables the grammar to disallow potentially confusing code such as

object o = new int[3][1];

which would otherwise be interpreted as

object o = (new int[3])[1];

7.5.1Literals


A primary-expression that consists of a literal (§2.4.4) is classified as a value.

7.5.2Simple names


A simple-name consists of an identifier, optionally followed by a type argument list:

simple-name:
identifier type-argument-listopt

A simple-name is either of the form I or of the form I1, ..., AK>, where I is a single identifier and 1, ..., AK> is an optional type-argument-list. When no type-argument-list is specified, consider K to be zero. The simple-name is evaluated and classified as follows:



  • If K is zero and the simple-name appears within a block and if the block’s (or an enclosing block’s) local variable declaration space (§3.3) contains a local variable, parameter or constant with name I, then the simple-name refers to that local variable, parameter or constant and is classified as a variable or value.

  • If K is zero and the simple-name appears within the body of a generic method declaration and if that declaration includes a type parameter with name I, then the simple-name refers to that type parameter.

  • Otherwise, for each instance type T (§10.3.1), starting with the instance type of the immediately enclosing type declaration and continuing with the instance type of each enclosing class or struct declaration (if any):

  • If K is zero and the declaration of T includes a type parameter with name I, then the simple-name refers to that type parameter.

  • Otherwise, if a member lookup (§7.3) of I in T with K type arguments produces a match:

  • If T is the instance type of the immediately enclosing class or struct type and the lookup identifies one or more methods, the result is a method group with an associated instance expression of this. If a type argument list was specified, it is used in calling a generic method (§7.5.5.1).

  • Otherwise, if T is the instance type of the immediately enclosing class or struct type, if the lookup identifies an instance member, and if the reference occurs within the block of an instance constructor, an instance method, or an instance accessor, the result is the same as a member access (§7.5.4) of the form this.I. This can only happen when K is zero.

  • Otherwise, the result is the same as a member access (§7.5.4) of the form T.I or T.I1, ..., AK>. In this case, it is a compile-time error for the simple-name to refer to an instance member.

  • Otherwise, for each namespace N, starting with the namespace in which the simple-name occurs, continuing with each enclosing namespace (if any), and ending with the global namespace, the following steps are evaluated until an entity is located:

  • If K is zero and I is the name of a namespace in N, then:

  • If the location where the simple-name occurs is enclosed by a namespace declaration for N and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with a namespace or type, then the simple-name is ambiguous and a compile-time error occurs.

  • Otherwise, the simple-name refers to the namespace named I in N.

  • If K is zero and the location where the simple-name occurs is enclosed by a namespace declaration for N and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with a namespace or type, then the simple-name is ambiguous and a compile-time error occurs.

  • Otherwise, the namespace-or-type-name refers to the type constructed with the given type arguments.

  • Otherwise, if the location where the simple-name occurs is enclosed by a namespace declaration for N:

  • If K is zero and the namespace declaration contains an extern-alias-directive or using-alias-directive that associates the name I with an imported namespace or type, then the simple-name refers to that namespace or type.

  • Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain exactly one type having name I and K type parameters, then the simple-name refers to that type constructed with the given type arguments.

  • Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration contain more than one type having name I and K type parameters, then the simple-name is ambiguous and an error occurs.

Note that this entire step is exactly parallel to the corresponding step in the processing of a namespace-or-type-name (§3.8).

  • Otherwise, the simple-name is undefined and a compile-time error occurs.

7.5.2.1Invariant meaning in blocks


For each occurrence of a given identifier as a simple-name in an expression or declarator, every other occurrence of the same identifier as a simple-name in an expression or declarator within the immediately enclosing block (§8.2) or switch-block (§8.7.2) must refer to the same entity. This rule ensures that the meaning of a name is always the same within a block.

The example

class Test
{
double x;

void F(bool b) {


x = 1.0;
if (b) {
int x;
x = 1;
}
}
}

results in a compile-time error because x refers to different entities within the outer block (the extent of which includes the nested block in the if statement). In contrast, the example

class Test
{
double x;

void F(bool b) {


if (b) {
x = 1.0;
}
else {
int x;
x = 1;
}
}
}

is permitted because the name x is never used in the outer block.

Note that the rule of invariant meaning applies only to simple names. It is perfectly valid for the same identifier to have one meaning as a simple name and another meaning as right operand of a member access (§7.5.4). For example:

struct Point


{
int x, y;

public Point(int x, int y) {


this.x = x;
this.y = y;
}
}

The example above illustrates a common pattern of using the names of fields as parameter names in an instance constructor. In the example, the simple names x and y refer to the parameters, but that does not prevent the member access expressions this.x and this.y from accessing the fields.




Download 3.2 Mb.

Share with your friends:
1   ...   25   26   27   28   29   30   31   32   ...   85




The database is protected by copyright ©ininet.org 2024
send message

    Main page