## Description

Represents the base type of another type or subtype. This attribute is used to access attributes of the base type.

T'Base refers to the "base range" of the type, which defines the range in which intermediate calculations are performed.

## Base for Integer Types

The standard states that the range of T'Base:

1. includes the value 0
2. is symmetric about zero, with possibly an extra negative value
3. includes all of the values in the subtype T

So for example, if T is:

``` .mw-parser-output .ada-kw{background:none;border:none;padding:0;margin:0;color:DodgerBlue;font-weight:bold}.mw-parser-output .ada-kw a,.mw-parser-output .ada-kw a:visited{color:inherit}`type` T `is` `range` 1 .. 42;
```

then the compiler will choose a hardware supported type that includes this range, in this case probably an eight bit type (on a two's complement machine)

``` `type` T'Base `is` `range` -128 .. 127;
```

This definition is not to be taken literally, as by Ada semantics, T and T'Base would be incompatible. In effect, the declaration of T would then be

``` `subtype` T `is` T'Base `range` 1 .. 42;
```

Note that built-in operators go through the base type, and T's "+" op for example is implicitly declared as:

```  `function` "+" (L, R : T'Base) `return` T'Base;
```

There are no constraint checks on T'Base, so for example:

``` `declare`
O1 : T      := T'(1) + T'(2);
O2 : T'Base := T'(1) + T'(2);
`begin`
```

then in the first assignment to O1, there is a constraint check to ensure that the result of 1 + 2 is in the range of T, but in the second assignment to O2, there is no check.

T'Base is useful for generics, when you need to be able to recover the base range of the type, in order to declare an object with value 0; for example, if this is an accumulator.

It is helpful to know something about the base range of the type, so that you have a guarantee that you don't get any overflow during intermediate calculations. For example, given type T above then:

``` `procedure` Op (O1, O2 : T) `is`
Sum : T'Base := O1 + O2;
`begin`
```

This is a problem, since if the sum of O1 and O2 is large (that is, greater than T'Base'Last), then you'll get overflow. Knowing that you're going to be adding two values together means you should declare the type this way:

```  T_Last : `constant` := 42;
`type` T_Base `is` 0 .. 2 * T_Last;
`subtype` T `is` T_Base `range` 1 .. T_Last;
```

That way you know that (sub)type T's range is 1 .. 42, but you also have a guarantee that T'Base'Last >= 84, and hence the sum of two values of type T cannot overflow.

Note that a declaration of the form:

```  `type` T `is` `range` ...
```

actually declares a subtype, named T, of some anonymous base type. We can refer to the range of this base type as T'Base.

## Base for Enumeration Types

An enumeration type is its own base type, so given this type:

```  `type` ET `is` (A, B, C);
```

then the range of ET is the same as the range of ET'Base. If you need some extra literals in your "base" type, then you have to declare them manually, not unlike what we did above:

``` `type` ET_Base `is` (ET_Base_First, A, B, C, ET_Base_Last);
`subtype` ET `is` ET_Base `range` A .. C;
```

Now you can say ET'Succ (ET'Last) and you'll get a meaningful answer. This is necessary when you do something like:

``` `declare`
E : ET'Base := ET'First;
`begin`
`while` E <= ET'Last `loop`
... --  do something
E := ET'Succ (E);
`end` `loop`;
`end`;
```

Also when you derive from ET_Base with a range constraint, the base of the derived type will include all values of the base type:

``` `type` New_ET `is` `new` ET_Base `range` A .. C;

Correct: `constant` Boolean := New_ET'Base'First = ET_Base_First;
```

Note that here ET_BASE_First is of type New_ET.

## Example

If you declare:

````type` My_Enum `is` (Enum1, Enum2, Enum3);
```

and

````subtype` Sub_Enum `is` My_Enum `range` Enum1 .. Enum2;
```

then Sub_Enum'Base'Last is Enum3.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8489174,"math_prob":0.9120252,"size":3512,"snap":"2020-24-2020-29","text_gpt3_token_len":965,"char_repetition_ratio":0.13568985,"word_repetition_ratio":0.0064102565,"special_character_ratio":0.26594535,"punctuation_ratio":0.17096336,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.97864676,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-25T11:13:51Z\",\"WARC-Record-ID\":\"<urn:uuid:e88991ba-6aa5-470b-8694-1c0dece2380b>\",\"Content-Length\":\"55361\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:44c7781e-8ed2-43ca-8069-2f427e914067>\",\"WARC-Concurrent-To\":\"<urn:uuid:4680daae-a1a3-45b2-b58c-5ddd60247855>\",\"WARC-IP-Address\":\"208.80.154.224\",\"WARC-Target-URI\":\"https://en.wikibooks.org/wiki/Ada_Programming/Attributes/%27Base\",\"WARC-Payload-Digest\":\"sha1:GRIZV2IQ2QFEFMWWOCUWOB3HH7PYKAYV\",\"WARC-Block-Digest\":\"sha1:MSJOUF6SF2WCB2OEPDB62FN6HY6VN46V\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347388427.15_warc_CC-MAIN-20200525095005-20200525125005-00077.warc.gz\"}"}