Initially assigned variables 


Мы поможем в написании ваших работ!



ЗНАЕТЕ ЛИ ВЫ?

Initially assigned variables



The following categories of variables are classified as initially assigned:

· Static variables.

· Instance variables of class instances.

· Instance variables of initially assigned struct variables.

· Array elements.

· Value parameters.

· Reference parameters.

· Variables declared in a catch clause or a foreach statement.

Initially unassigned variables

The following categories of variables are classified as initially unassigned:

· Instance variables of initially unassigned struct variables.

· Output parameters, including the this variable of struct instance constructors.

· Local variables, except those declared in a catch clause or a foreach statement.

Precise rules for determining definite assignment

In order to determine that each used variable is definitely assigned, the compiler must use a process that is equivalent to the one described in this section.

The compiler processes the body of each function member that has one or more initially unassigned variables. For each initially unassigned variable v, the compiler determines a definite assignment state for v at each of the following points in the function member:

· At the beginning of each statement

· At the end point (§8.1) of each statement

· On each arc which transfers control to another statement or to the end point of a statement

· At the beginning of each expression

· At the end of each expression

The definite assignment state of v can be either:

· Definitely assigned. This indicates that on all possible control flows to this point, v has been assigned a value.

· Not definitely assigned. For the state of a variable at the end of an expression of type bool, the state of a variable that isn’t definitely assigned may (but doesn’t necessarily) fall into one of the following sub-states:

o Definitely assigned after true expression. This state indicates that v is definitely assigned if the boolean expression evaluated as true, but is not necessarily assigned if the boolean expression evaluated as false.

o Definitely assigned after false expression. This state indicates that v is definitely assigned if the boolean expression evaluated as false, but is not necessarily assigned if the boolean expression evaluated as true.

The following rules govern how the state of a variable v is determined at each location.

General rules for statements

· v is not definitely assigned at the beginning of a function member body.

· v is definitely assigned at the beginning of any unreachable statement.

· The definite assignment state of v at the beginning of any other statement is determined by checking the definite assignment state of v on all control flow transfers that target the beginning of that statement. If (and only if) v is definitely assigned on all such control flow transfers, then v is definitely assigned at the beginning of the statement. The set of possible control flow transfers is determined in the same way as for checking statement reachability (§8.1).

· The definite assignment state of v at the end point of a block, checked, unchecked, if, while, do, for, foreach, lock, using, or switch statement is determined by checking the definite assignment state of v on all control flow transfers that target the end point of that statement. If v is definitely assigned on all such control flow transfers, then v is definitely assigned at the end point of the statement. Otherwise; v is not definitely assigned at the end point of the statement. The set of possible control flow transfers is determined in the same way as for checking statement reachability (§8.1).

Block statements, checked, and unchecked statements

The definite assignment state of v on the control transfer to the first statement of the statement list in the block (or to the end point of the block, if the statement list is empty) is the same as the definite assignment statement of v before the block, checked, or unchecked statement.

Expression statements

For an expression statement stmt that consists of the expression expr:

· v has the same definite assignment state at the beginning of expr as at the beginning of stmt.

· If v if definitely assigned at the end of expr, it is definitely assigned at the end point of stmt; otherwise; it is not definitely assigned at the end point of stmt.

Declaration statements

· If stmt is a declaration statement without initializers, then v has the same definite assignment state at the end point of stmt as at the beginning of stmt.

· If stmt is a declaration statement with initializers, then the definite assignment state for v is determined as if stmt were a statement list, with one assignment statement for each declaration with an initializer (in the order of declaration).

If statements

For an if statement stmt of the form:

if (expr) then-stmt else else-stmt

· v has the same definite assignment state at the beginning of expr as at the beginning of stmt.

· If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to then-stmt and to either else-stmt or to the end-point of stmt if there is no else clause.

· If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely assigned on the control flow transfer to then-stmt, and not definitely assigned on the control flow transfer to either else-stmt or to the end-point of stmt if there is no else clause.

· If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely assigned on the control flow transfer to else-stmt, and not definitely assigned on the control flow transfer to then-stmt. It is definitely assigned at the end-point of stmt if and only if it is definitely assigned at the end-point of then-stmt.

· Otherwise, v is considered not definitely assigned on the control flow transfer to either the then-stmt or else-stmt, or to the end-point of stmt if there is no else clause.

Switch statements

In a switch statement stmt with a controlling expression expr:

· The definite assignment state of v at the beginning of expr is the same as the state of v at the beginning of stmt.

· The definite assignment state of v on the control flow transfer to a reachable switch block statement list is the same as the definite assignment state of v at the end of expr.

While statements

For a while statement stmt of the form:

while (expr) while-body

· v has the same definite assignment state at the beginning of expr as at the beginning of stmt.

· If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to while-body and to the end point of stmt.

· If v has the state “definitely assigned after true expression” at the end of expr, then it is definitely assigned on the control flow transfer to while-body, but not definitely assigned at the end-point of stmt.

· If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt, but not definitely assigned on the control flow transfer to while-body.

Do statements

For a do statement stmt of the form:

do do-body while (expr);

· v has the same definite assignment state on the control flow transfer from the beginning of stmt to do-body as at the beginning of stmt.

· v has the same definite assignment state at the beginning of expr as at the end point of do-body.

· If v is definitely assigned at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.

· If v has the state “definitely assigned after false expression” at the end of expr, then it is definitely assigned on the control flow transfer to the end point of stmt.

For statements

Definite assignment checking for a for statement of the form:

for (for-initializer; for-condition; for-iterator) embedded-statement

is done as if the statement were written:

{
for-initializer;
while (for-condition) {
embedded-statement;
for-iterator;
}
}

If the for-condition is omitted from the for statement, then evaluation of definite assignment proceeds as if for-condition were replaced with true in the above expansion.



Поделиться:


Последнее изменение этой страницы: 2016-08-10; просмотров: 224; Нарушение авторского права страницы; Мы поможем в написании вашей работы!

infopedia.su Все материалы представленные на сайте исключительно с целью ознакомления читателями и не преследуют коммерческих целей или нарушение авторских прав. Обратная связь - 18.191.211.66 (0.01 с.)