DEVELOPMENT EDITION · 769442725406
Vada syntax
On this page

Vada declarations and types

A declaration introduces a name, its type or other role, and sometimes an initial value. Declarations occur in package specifications/bodies, subprogram bodies and nested declarative blocks. They are not executable statements placed anywhere in a statement sequence.

The shared type reference, composite types and ownership rules define the meaning of these forms. Examples below are declaration fragments unless linked as complete files.

Objects, constants and named numbers

Count : Natural := 0;
Left, Right : Integer;
Limit : constant Positive := 100;
Capacity : constant := 100;

Count is mutable. Left and Right are separate objects with the same declared subtype; flow analysis must establish initialization before reading them. Limit is a typed constant. Capacity is a named number, whose expression must be static; omitting its type does not introduce general local type inference.

The object pattern is:

names ":" ["aliased"] ["constant"] object-type
      [":=" expression] [aspect-specification] ";"

A constant ordinarily needs an initializer. Deferred constants are permitted in their appropriate package interface/private completion arrangement; they are not uninitialized mutable variables. aliased permits supported access-taking subject to ownership and accessibility checks. It does not disable alias analysis.

Storage : aliased Integer := 0;

Objects can have constrained subtype indications such as String(1 .. 20), Vector(0 .. N) or Variant(Kind => Small). An unconstrained object can acquire supported bounds/discriminants from its initialization. Do not assume every indefinite object can exist without such a constraint or value.

Scalar types and subtypes

Declaration fragment Meaning
type State is (Idle, Busy, Done); New enumeration type
type Small is range -100 .. 100; New signed integer type
subtype Percent is Integer range 0 .. 100; Constrained subtype, retaining Integer's identity
type Byte is mod 256; Modular integer, values 0 through 255
type Distance is digits 6 range -1.0E6 .. 1.0E6; Floating-point type with the declared precision/range
type Ratio is delta 0.01 range -1000.0 .. 1000.0; Ordinary fixed-point type
type Money is delta 0.01 digits 12; Decimal fixed-point type: exact steps of 0.01, 12 digits
type Metres is new Integer range 0 .. 1000; Distinct derived type

Signed type bounds and modular moduli must be static and supported. Natural and Positive are constrained subtypes of Integer. Derived/new types are not interchangeable merely because their representations or ranges match.

Enumeration literals can be identifiers or character literals. A literal can be overloaded between types; its context must determine which type is intended. Numeric literals similarly acquire a specific type from context.

Fixed-point delta describes a requested accuracy; the implementation's Small and rounding rules determine representation. Do not infer decimal arithmetic from a decimal literal in the declaration. A decimal fixed-point type, delta followed by digits, has a power-of-ten delta that is also its small, so its values are exact decimal steps. Use numeric semantics for precision and conversion behavior.

A subtype indication is a type/subtype name, optionally not null and optionally constrained. A range constraint can use explicit bounds or an appropriate 'Range attribute. It narrows the subtype it constrains: its bounds must belong to that subtype, unless the range is empty (RM 3.2.2). Static bounds outside it are an error (B2002), as in subtype P is Positive range -5 .. 5;; bounds known only at run time are checked where the declaration or statement that has them is elaborated, a range check the prover must prove (B4102): an object's or subtype's constraint, a loop's or a quantifier's subtype indication. A constraint whose bounds are known only inside an expression (an enclosing quantifier's variable, a declare expression's constant) is checked there, before the part that has it, and so is one in a contract: the prover proves it, given what the contract states before it, and --advisory checks it when the contract is evaluated. A record component's constraint that names a discriminant is checked where a value of the record is made with the discriminants' values: an aggregate, an object initialised by default (RM 3.8(18)). Parenthesized constraints provide array bounds or discriminants. A digits constraint gives a decimal fixed-point subtype fewer digits, and with them a narrower range: subtype Price is Money digits 6; runs from -9999.99 through 9999.99, and Money digits 4 range 0.0 .. 10.0 takes the range it states. A digits constraint on a floating-point subtype and a delta constraint are Annex J.3's obsolescent forms, rejected with B0904.

Arrays

type Four_Scores is array (1 .. 4) of Integer;
type Scores is array (Positive range <>) of Integer;
type Matrix is array (Positive range <>, Positive range <>) of Integer;

The first array has fixed bounds. range <> leaves bounds to each constrained use. The index subtype still restricts legal bounds/indices. Each dimension of a multidimensional array has its own bounds; the value is rectangular.

A : Four_Scores := [10, 20, 30, 40];
B : Scores(5 .. 7) := [1, 2, 3];
M : Matrix(1 .. 2, 1 .. 3) := [[1, 2, 3], [4, 5, 6]];

These fragments assume their preceding declarations. Lower bounds need not be 0 or 1 for every array. Whole-array assignment can slide compatible source lengths into destination bounds; it does not silently resize a constrained destination. String is a predefined one-dimensional array of Latin-1 Character indexed by Positive.

Anonymous object arrays use the same definition directly:

Flags : array (1 .. 4) of Boolean := [others => False];

A component can be aliased where supported. Arrays can have composite components. Use M(I, J) for multidimensional indexing, not repeated row indexing when M is one multidimensional array.

Records and discriminants

type Point is record
   X, Y : Integer := 0;
end record;

A component declaration resembles an object declaration and can supply a default initializer and aspects. Record components belong to each object. A record aggregate supplies values; a type declaration does not instantiate an object.

A discriminant is a value that controls a record's shape or variants:

type Message_Kind is (Number, Empty);
type Message (Kind : Message_Kind := Empty) is record
   case Kind is
      when Number =>
         Value : Integer;
      when Empty =>
         null;
   end case;
end record;

A record may have ordinary components followed by a variant part. Each variant has choices and its own components; a nested variant is represented inside its component list. Only components present for the actual discriminant can be selected. Discriminant checks, aggregate coverage, constraints and assignment rules are proved or rejected as applicable. Unknown-discriminant notation (<>) belongs to supported incomplete/private/formal contexts, not arbitrary runtime tuple arguments.

Defaults permit the supported mutable-discriminant forms. Storage size is bounded by the implementation's layout limits; an unbounded maximum variant allocation is not implied. Access discriminants are excluded.

Discriminants also stand on four other kinds of declaration:

type Buffer (<>) is private;                       --  unknown discriminants
type Node (Size : Natural);                        --  an incomplete type
type Filled (Count : Positive) is new Buffer_Record (Length => Count);
type Polygon (Sides : Positive) is new Shape with record
   Lengths : Values (1 .. Sides);
end record;

A private or incomplete type with known discriminants is completed by a record that repeats them, the same names of the same subtypes in the same order (B1015). One with unknown discriminants, (<>), may be completed by any type; outside the package its subtype is indefinite, so an object of it takes its value, and with it its discriminants, from its initial value (B2013), it takes no constraint, and no array or record has a component of it. A generic formal type T (<>) is private takes indefinite actuals the same way.

A derived type may declare discriminants of its own. Its parent subtype is constrained: each parent discriminant is given one of the new discriminants, alone, in a subtype within the parent's (Count : Positive for Length : Natural, B2002), or a value; an untagged type's new discriminants each constrain at least one. The parent's discriminants are not inherited:

type Grid (Rows, Columns : Natural) is record ... end record;
type Row (Length : Natural) is new Grid (Rows => 1, Columns => Length);
type Square (Side : Natural) is new Grid (Side, Side);
type Turned (Columns, Rows : Natural) is new Grid (Rows => Rows, Columns => Columns);

A Row has the one discriminant Length; its rows are 1, which no name reaches but every value holds, so Grid (R) has Rows = 1. Row'(Length => 3, Filled => 0) and Row (3) name only Length, and Turned (3, 4) has 3 columns. A conversion from the parent checks that each discriminant lies in the derived type's subtype and that the others are what the type gives them (Row (G) needs G.Rows = 1, Square (G) needs G.Rows = G.Columns). A type extension may also declare discriminants when its parent has none: its own discriminants come after the parent's components, which keep their places, so views of the parent and class-wide values are unchanged. A tagged type that is not limited gives its discriminants no defaults.

Private, incomplete and limited types

type Node;
type Handle is private;
type Resource is limited private;

An incomplete declaration supports forward references in permitted positions; it must later receive an appropriate full declaration. A private declaration exposes a type's public view while its representation is completed in the package's private part. Clients cannot use hidden fields because the full representation exists elsewhere in the same project.

limited restricts copying and assignment. It is useful for owned resources whose identity and cleanup cannot be duplicated by ordinary value copying. A private type's completion must conform to its declared visible properties.

A minimal private-view pattern is:

package Points is
   type Point is private;
private
   type Point is record
      X, Y : Integer := 0;
   end record;
end Points;

A body is required only when declarations or elaboration require one; declaring a package body without an allowed reason can itself be diagnosed.

Tagged types, extensions and interfaces

type Shape is abstract tagged null record;
type Circle is new Shape with record
   Radius : Natural;
end record;
type Printable is interface;

These are type-declaration fragments. A tagged type supports a declared hierarchy and dispatching primitive operations. T'Class is the class-wide type covering supported descendants. An abstract type/operation cannot be directly instantiated/called as a concrete implementation.

An extension can add interfaces with and, then with record ... end record or with null record. A private extension uses with private in the public view. Override indicators belong to subprogram declarations: overriding or not overriding.

Plain and limited interfaces are available; synchronized, task and protected interfaces are excluded. See subprograms for dispatch and class-wide contracts.

Access and callback types

type Link is access Node;
type Reader is access function (Value : Integer) return Integer;
type Action is not null access procedure (Value : in out Integer);

An access-to-object type ordinarily owns its designated object. A callback access type designates a compatible subprogram and may declare contracts governing indirect calls. not null excludes the null value at a checked boundary.

Anonymous access parameters and objects borrow and observe rather than share. A general access type, type Ref is access all T;, designates an aliased declared object under the general access rules. 'Unchecked_Access, 'Unrestricted_Access and access discriminants are excluded. See ownership syntax and the shared callback rules.

Renamings and exception names

An object renaming names an existing object without copying it:

Current : Integer renames Values(Index);
Same renames Current;

The second is the supported typeless renaming form. Both obey lifetime and alias rules. What is renamed may also be a slice (Middle : String renames Line (3 .. 5);, its bounds fixed where it is declared), a view conversion of a tagged object (R : Root renames Root (V);), a qualified expression of an object (a constant view of it), or the object an access value designates (X : Integer renames A.all;). The last borrows A while the renaming lives, as an anonymous access object would: A is not used meanwhile, and what is written through X is in A.all afterwards (ownership). A package renaming uses package Short renames Long.Package_Name;. It can be a library unit of its own: a file short.vads holding with Long.Package_Name; and the renaming lets other units with Short; and call Short.Answer. Subprogram and exception renamings use their own declaration categories:

procedure Visit (Value : Integer) renames Walk;
Bad_Input : exception;
Input_Error : exception renames Bad_Input;

These are context-dependent fragments; matching declarations and profiles must exist. An exception name identifies a user exception, not a numeric status value.

Representation source forms

Supported aspects/clauses can constrain representation or interoperability. The spellings are:

type-declaration with Size => expression;
for Type_Name'Size use expression;
for Enum_Type use (Literal => Code, ...);
for Record_Type use record
   Field at Byte_Position range First_Bit .. Last_Bit;
end record;

These are accepted for compatibility and do not change the layout Verada chooses; see representation clauses.

Use a modern Address aspect or for Object'Address use ...; for supported external objects. The older for Object use at ...; form and at mod alignment clause are diagnosed as obsolescent. Ada.Unchecked_Conversion reinterprets scalars of the same size, with a check that the bits are a value of the target (see types).

Declaration grammar map

declaration ::= object | named-number | type | subtype | renaming | exception
              | subprogram | package | generic | representation | use | pragma
              | task | protected-object | protected-entry
type        ::= "type" name [discriminants] ["is" definition] [aspects] ";"
subtype     ::= "subtype" name "is" subtype-indication [aspects] ";"
definition  ::= enumeration | signed | modular | floating | ordinary-fixed
              | decimal-fixed | array | record | derived | private | interface | access

Formal generic types add box forms such as range <>. Some definitions recognized for a specific diagnostic are excluded; the grammar is organized by the supported declaration families above rather than importing all of Ada as an unstated dependency.