29.4.2 CASE Statements

Because pointers and the data types INTN and UINTN are different sizes on different instruction set architectures and case statements are determined at compile time; the sizeof() function cannot be used in a case statement with an indeterminately sized data type because the sizeof() function cannot be evaluated to a constant by the EBC compiler at compile time. UEFI status codes values such as EFI_SUCCESS and EFI_UNSUPPORTED are defined differently on different CPU architectures. As a result, UEFI status codes cannot be used in case expressions. The following example shows examples using case statements.

Example 253-Case statements that fail for EBC

#include <Uefi.h>
UINTN Value;
switch (Value) {
case 0: // Works because 0 is a constant.
break;
case sizeof (UINT16): // Works because sizeof (UINT16) is always 2
break;
case sizeof (UINTN): // EBC compiler error. sizeof (UINTN) is not constant.
break;
case EFI_UNSUPPORTED: // EBC compiler error. EFI_UNSUPPORTED is not constant.
break;
}

One solution to this issue is to convert case statements into if/else statements. The example below shows the equivalent functionality as Example 253, above, but does not generate any EBC compiler errors.

Example 254-Case statements that work for EBC

#include <Uefi.h>
UINTN Value;
switch (Value) {
case 0: // Works because 0 is a constant.
break;
case sizeof (UINT16): // Works because sizeof (UINT16) is always 2
break;
}
if (Value == sizeof (UINTN)) {
} else if (Value == EFI_UNSUPPORTED) {
}