Previous: Attributes, Up: Project File Reference


11.7.10 Case Statements

A case statement is used in a project file to effect conditional behavior. Through this statement, you can set the value of attributes and variables depending on the value previously assigned to a typed variable.

All choices in a choice list must be distinct. Unlike Ada, the choice lists of all alternatives do not need to include all values of the type. An others choice must appear last in the list of alternatives.

The syntax of a case construction is based on the Ada case statement (although the null statement for empty alternatives is optional).

The case expression must be a typed string variable, whose value is often given by an external reference (see External Values).

Each alternative starts with the reserved word when, either a list of literal strings separated by the "|" character or the reserved word others, and the "=>" token. Each literal string must belong to the string type that is the type of the case variable. After each =>, there are zero or more statements. The only statements allowed in a case construction are other case statements, attribute declarations and variable declarations. String type declarations and package declarations are not allowed. Variable declarations are restricted to variables that have already been declared before the case construction.

     case_statement ::=
       case <typed_variable_>name is {case_item} end case ;
     
     case_item ::=
       when discrete_choice_list =>
         {case_statement
           | attribute_declaration
           | variable_declaration
           | empty_declaration}
     
     discrete_choice_list ::= string_literal {| string_literal} | others

Here is a typical example:

     project MyProj is
        type OS_Type is ("GNU/Linux", "Unix", "NT", "VMS");
        OS : OS_Type := external ("OS", "GNU/Linux");
     
        package Compiler is
          case OS is
            when "GNU/Linux" | "Unix" =>
              for Switches ("Ada") use ("-gnath");
            when "NT" =>
              for Switches ("Ada") use ("-gnatP");
            when others =>
              null;
          end case;
        end Compiler;
     end MyProj;