Ada-95: A guide for C and C++ programmers by Simon Johnston 1995 Welcome ... to the Ada guide especially written for C and C++ programmers. Summary I have endeavered to present below a tutorial for C and C++ programmers to show them what Ada can provide and how to set about turning the knowledge and experience they have gained in C/C++ into good Ada programming. This really does expect the reader to be familiar with C/C++, although C only programmers should be able to read it OK if they skip section 3. My thanks to S. Tucker Taft for the mail that started me on this. 1 Contents 1 Ada Basics. 1.1 C/C++ types to Ada types. . . . . . . . . . 1.1.1 Declaring new types and subtypes. . . . . . 1.1.2 Simple types, Integers and Characters. . 1.1.3 Strings. {3.6.3} . . . . . . . . . . . . . . . . . 1.1.4 Floating {3.5.7} and Fixed {3.5.9} point. . 1.1.5 Enumerations {3.5.1} and Ranges. 1.1.6 Arrays {3.6}. . . . . . . 1.1.7 Records {3.8}. . . . . . . . . . . . . . 1.1.8 Access types (pointers) {3.10}. . 1.1.9 Ada advanced types and tricks. . . . . . . . . . . . . . . . 1.1.10 C Unions in Ada, (food for thought). . 1.2 C/C++ statements to Ada. . . . . . . 1.2.1 Compound Statement {5.6} . 1.2.2 if Statement {5.3} . . . . 1.2.3 switch Statement {5.4} . 1.2.4 Ada loops {5.5} . . . . . 1.2.4.1 while Loop . 1.2.4.2 do Loop . . 1.2.4.3 for Loop . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.4.4 break and continue . 1.2.5 return {6.5} . . . . . . . 1.2.6 labels and goto {5.8} . . 1.2.7 exception handling {11.2} 1.2.8 sub-programs . . . . . . 1.3 Ada Safety. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 8 8 9 10 10 11 13 15 16 18 22 23 24 24 25 26 26 26 26 27 28 29 29 30 33 CONTENTS CONTENTS 1.3.1 Static provability. . . . . . . . . . . 1.3.2 Predefined exceptions and pragmas. 1.3.3 Unchecked programming. . . . . . 2 Ada Packages. {7} 2.1 What a package looks like . . . 2.2 Include a package in another . . 2.3 Package data hiding {7.3} . . . . 2.4 Hierarchical packages. . . . . 2.5 Renaming identifiers. . . . . . . . . . . . . . . . . . 3 Ada-95 Object Oriented Programming. 3.1 The tagged type. . . . . . . . . . 3.2 Class member attributes. . . . . 3.3 Class member functions. . . . . 3.4 Virtual member functions. . . . 3.5 Static members. . . . . . . . . . 3.6 Constructors/Destructors for Ada. 3.7 3.8 Inheritance, single and multiple. public/protected/private. . . . . 3.9 A more complete example. . . . 4 Generics 4.1 A generic procedure {12.6} . . . 4.2 Generic packages {12.7} . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Generic types and other parameters {12.4} . 5 IO 5.1 Ada.Text_IO . . . . . . . . . . . . . . 5.2 Ada.Sequential_IO and Ada.Direct_IO . 5.3 Streams . . . . . . . . . . . . . . . . . . . . . . . 6 Interfacing to other languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 34 35 37 37 38 39 40 41 43 44 44 45 45 45 45 45 46 46 49 49 50 51 52 52 53 55 56 3 CONTENTS 7 Concurrency CONTENTS 57 57 57 57 58 58 58 60 61 61 63 63 64 64 7.1 Tasks . . . . . . . . . . . . . . 7.1.1 Tasks as threads . . . . . 7.1.2 A Simple task . . . . . . 7.1.3 Task as types . . . . . . . . . . 7.2 Task synchronization (Rendezvouz) 7.2.1 entry/accept . . . . . . . 7.2.2 select . . . . . . . . . . 7.2.3 guarded entries . . . . . 7.2.4 delays . . . . . . . . . . 7.2.5 select else . . . . . . . . 7.2.6 termination . . . . . . . 7.2.7 conditional entry calls . 7.3 Protected types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Introduction. This document is written primarily for C and C++ programmers and is set out to describe the Ada programming language in a way more accessible to them. I have used the standard Ada documentation conventions, code will look like this and keywords will look like this. I will include references to the Ada Reference Manual in braces and in italics, {1.1}, which denotes section 1.1. The ARM is reference 1 at the end of this document. Another useful reference is the Lovelace on-line tutorial which is a great way to pick up Ada basics. I will start out by describing the Ada predefined types, and the complex types, and move onto the simple language constructs. Section 2 will start to introduce some very Ada specific topics and section 3 describes the new Ada-95 Object Oriented programming constructs. Section 5 describes the Ada tools for managing concurrency, the task and protected types, these are worth investing some time getting to grips with. Section 6 is a tour of the Ada IO library and covers some of the differences in concept and implementation between it and C stdio. Please feel free to comment on errors, things you don’t like and things you would like to see. If I don’t get the comments then I can’t take it forward, and the question you would like answered is almost certainly causing other people problems too. If you are new to Ada and do not have an Ada compiler handy then why not try the GNAT Ada compiler. This compiler is based on the well known GCC C/C++ and Objective-C compiler and provides a high quality Ada-83 and Ada-95 compiler for many platforms. Here is the FTP site (ftp: //cs.nyu.edu/pub/gnat) see if there is one for you. 5 Document Status. This document is still under revision and I receive a number of mails asking for improvements and fixing bugs and spelling mistakes I have introduced. I will try and keep this section up to date on what needs to be done and what I would like to do. Current Status Section 2 More on 2.3 (data hiding) and 2.4 (Hierarchical packages) Section 3 First issue of this section, 3.6, 3.7, 3.8 and 3.9 have additional work planned. They may also require re-work pending comments. Section 5 Section 5.3 (streams) not yet done. Section 6 New sections to be added for each language. Section 7 Major re-work following comments from Bill Wagner, 7.2.7 added, requires some more words, and section 7.3 requires more justification etc. Wish List I would like to use a consistant example throughout, building it up as we go along. The trouble is I don’t think I have space in an HTML page to do this. 6 1 Ada Basics. This section hopes to give you a brief introduction to Ada basics, such as types, statements and packages. Once you have these you should be able to read quite a lot of Ada source without difficulty. You are expected to know these things as we move on so it is worth reading. One thing before we continue, most of the operators are similar, but you should notice these differ- ences: Operator Assignment Equality NonEquality PlusEquals SubtractEquals MultiplyEquals DivisionEquals OrEquals AndEquals Modulus Remainder AbsoluteValue Exponentiation Range C/C++ Ada := = /= = == != += -= *= /= |= &= % mod rem abs ** . . One of the biggest things to stop C/C++ programmers in their tracks is that Ada is case insensitive, so begin BEGIN Begin are all the same. This can be a problem when porting case sensitive C code into Ada. Another thing to watch for in Ada source is the use of ’ the tick. The tick is used to access attributes for an object, for instance the following code is used to assign to value a the size in bits of an integer. int a = sizeof(int) * 8; a : Integer := Integer’Size; Another use for it is to access the attributes First and Last, so for an integer the range of possible values is Integer’First to Integer’Last. This can also be applied to arrays so if you are 7 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. passed an array and don’t know the size of it you can use these attribute values to range over it in a loop (see section 1.1.5 on page 11). The tick is also used for other Ada constructs as well as attributes, for example character literals, code statements and qualified expressions ( 1.1.8 on page 16). 1.1 C/C++ types to Ada types. This section attempts to outline how to move C/C++ type declarations into an Ada program and help you understand Ada code. Section 1.1.8 introduces some Ada specific advanced topics and tricks you can use in such areas as bit fields, type representation and type size. Note that ’objects’ are defined in reverse order to C/C++, the object name is first, then the object type, as in C/C++ you can declare lists of objects by seperating them with commas. int i; int a, b, c; int j = 0; int k, l = 1; i : Integer; a, b, c : Integer; j : Integer := 0; k, l : Integer := 1; The first three declarations are the same, they create the same objects, and the third one assigns j the value 0 in both cases. However the fourth example in C leaves k undefined and creates l with the value 1. In the Ada example it should be clear that both k and l are assigned the value 1. Another difference is in defining constants. const int days_per_week = 7; days_per_week : constant Integer := 7; days_per_week : constant := 7; In the Ada example it is possible to define a constant without type, the compiler then chooses the most appropriate type to represent it. 1.1.1 Declaring new types and subtypes. Before we delve into descriptions of the predefined Ada types it is important to show you how Ada defines a type. Ada is a strongly typed language, in fact possibly the strongest. This means that its type model is strict and absolutely stated. In C the use of typedef introduces a new name which can be used as a new type, though the weak typing of C and even C++ (in comparison) means that we have only really introduced a very poor synonym. Consider: typedef int INT; INT a; int b; a = b; // works, no problem 8 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. The compiler knows that they are both ints. Now consider: type INT is new Integer; a : INT; b : Integer; a := b; -- fails. The important keyword is new, which really sums up the way Ada is treating that line, it can be read as "a new type INT has been created from the type Integer", whereas the C line may be interpreted as "a new name INT has been introduced as a synonym for int". This strong typing can be a problem, and so Ada also provides you with a feature for reducing the distance between the new type and its parent, consider: subtype INT is Integer; a : INT; b : Integer; a := b; -- works. The most important feature of the subtype is to constrain the parent type in some way, for example to place an upper or lower boundary for an integer value (see section below on ranges). 1.1.2 Simple types, Integers and Characters. We have seen above the Integer type, there are a few more with Ada, these are listed below. Integer, Long_Integer etc. Any Ada compiler must provide the Integer type, this is a signed integer, and of implementation defined size. The compiler is also at liberty to provide Long_Integer, Short_Integer, Long_Long_Integer etc as needed. Unsigned Integers Ada does not have a defined unsigned integer, so this can be synthesised by a range type (see section 1.1.5), and Ada-95 has a defined package, System.Unsigned_Types which provide such a set of types. Ada-95 has added a modular type which specifies the maximum value, and also the feature that arithmatic is cyclic, underflow/overflow cannot occur. This means that if you have a modular type capable of holding values from 0 to 255, and its current value is 255, then incrementing it wraps it around to zero. Contrast this with range types (previously used to define unsigned integer types) in section 1.1.5 below. Such a type is defined in the form: type BYTE is mod 256; type BYTE is mod 2**8; The first simply specifies the maximum value, the second specifies it in a more ’precise’ way, and the 2**x form is often used in system programming to specify bit mask types. Note: it is not required to use 2**x, you can use any value, so 10**10 is legal also. Character {3.5.2} This is very similar to the C char type, and holds the ASCII character set. However it is actually defined in the package Standard {A.1} as an enumerated type (see section 1.1.5). There is an Ada equivalent of the C set of functions in ctype.h which is the package Ada.Characters.Handling. Ada Also defines a Wide_Character type for handling non ASCII character sets. 9 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. Boolean {3.5.3} This is also defined in the package Standard as an enumerated type (see below) as (FALSE, TRUE). 1.1.3 Strings. {3.6.3} Heres a god send to C/C++ programmers, Ada has a predefined String type (defined again in Standard). There is a good set of Ada packages for string handling, much better defined than the set provided by C, and Ada has a & operator for string concatenation. As in C the basis for the string is an array of characters, so you can use array slicing (see below) to extract substrings, and define strings of set length. What, unfortunatly, you cannot do is use strings as unbounded objects, hence the following. type A_Record is record illegal : String; legal : String(1 .. 20); end record; procedure check(legal : in String); The illegal structure element is because Ada cannot use ’unconstrained’ types in static declarations, so the string must be constrained by a size. Also note that the lower bound of the size must be greater than or equal to 1, the C/C++ array[4] which defines a range 0..3 cannot be used in Ada, 1..4 must be used. One way to specify the size is by initialisation, for example: Name : String := "Simon"; is the same as defining Name as a String(1..5) and assigning it the value "Simon" seperatly.. For parameter types unconstrained types are allowed, similar to passing int array[] in C. To overcome the constraint problem for strings Ada has a predefined package Ada.Strings.Unbounded which implements a variable length string type. 1.1.4 Floating {3.5.7} and Fixed {3.5.9} point. Ada has two non-integer numeric types, the floating point and fixed point types. The predefined floating point type is Float and compilers may add Long_Float, etc. A new Float type may be defined in one of two ways: type FloatingPoint1 is new Float; type FloatingPoint2 is digits 5; The first simply makes a new floating point type, from the standard Float, with the precision and size of that type, regardless of what it is. The second line asks the compiler to create a new type, which is a floating point type "of some kind" with a minimum of 5 digits of precision. This is invaluable when doing numeric intensive operations 10 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. and intend to port the program, you define exactly the type you need, not what you think might do today. If we go back to the subject of the tick, you can get the number of digits which are actually used by the type by the attribute ’Digits. So having said we want a type with minimum of 5 digits we can verify this: number_of_digits : Integer := FloatingPoint2’Digits; Fixed point types are unusual, there is no predefined type ’Fixed’ and such type must be declared in the long form: type Fixed is delta 0.1 range -1.0..1.0; This defines a type which ranges from -1.0 to 1.0 with an accuracy of 0.1. Each element, accuracy, low-bound and high-bound must be defined as a real number. There is a specific form of fixed point types (added by Ada-95) called decimal types. These add a clause digits, and the range clause becomes optional. type Decimal is delta 0.01 digits 10; This specifies a fixed point type of 10 digits with two decimal places. The number of digits includes the decimal part and so the maximum range of values becomes -99,999,999.99. . . +99,999,999.99 1.1.5 Enumerations {3.5.1} and Ranges. Firstly enumerations. These are not at all like C/C++s enums, they are true sets and the fact that the Boolean type is in fact: type Boolean is (FALSE, TRUE); should give you a feeling for the power of the type. You have already seen a range in use (for strings), it is expressed as low .. of the most useful ways of expressing interfaces and parameter values, for example: high and can be one type Hours type Hours24 is range 0 .. 23; type Minutes is range 1 .. 60; is new Integer range 1 .. 12; There is now no way that a user can pass us an hour outside the range we have specified, even to the extent that if we define a parameter of type Hours24 we cannot assign a value of Hours even though it can only be in the range. Another feature is demonstrated, for Hours we have said we want to restrict an Integer type to the given range, for the next two we have asked the compiler to choose a type it feels appropriate to hold the given range, this is a nice way to save a little finger tapping, but should be avoided Ada provides you a perfect environment to specify precisely what you want, use it the first definition leaves nothing to the imagination. Now we come to the rules on subtypes for ranges, and we will define the two Hours again as follows: 11 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. type Hours24 subtype Hours is Hours24 range 1..12; is new range 0..23; This limits the range even further, and as you might expect a subtype cannot extend the range beyond its parent, so range 0 .. 25 would have been illegal. Now we come to the combining of enumerations and ranges, so that we might have: type All_Days is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday); subtype Week_Days is All_Days range Monday .. Friday; subtype Weekend is All_Days range Saturday .. Sunday; We can now take a Day, and see if we want to go to work: Day : All_Days := Today; if Day in Week_Days then go_to_work; end if; Or you could use the form if Day in range Monday .. extra types. Friday and we would not need the Ada provides four useful attributes for enumeration type handling, note these are used slightly differ- ently than many other attributes as they are applied to the type, not the object. Succ This attribute supplies the ’successor’ to the current value, so the ’Succ value of an object containing Monday is Tuesday. Note: If the value of the object is Sunday then an exception is raised, you cannot Succ past the end of the enumeration. Pred This attribute provides the ’predecessor’ of a given value, so the ’Pred value of an object containing Tuesday is Monday. Note: the rule above still applies ’Pred of Monday is an error. Val This gives you the value (as a member of the enumeration) of element n in the enumeration. Thus Val(2) is Wednesday. Note: the rule above still applies, and note also that ’Val(0) is the same as ’First. Pos This gives you the position in the enumeration of the given element name. Thus ’Pos(Wednesday) is 2. Note: the range rules still apply, also that ’Last will work, and return Sunday. All_Days’Succ(Monday) = Tuesday All_Days’Pred(Tuesday) = Monday All_Days’Val(0) = Monday All_Days’First = Monday All_Days’Val(2) = Wednesday All_Days’Last = Sunday All_Days’Succ(All_Days’Pred(Tuesday)) = Tuesday 12 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. Ada also provides a set of 4 attributes for range types, these are intimatly associated with those above and are: First This provides the value of the first item in a range. Considering the range 0 .. 100 then ’First is 0. Last This provides the value of the last item in a range, and so considering above, ’Last is 100. Length This provides the number of items in a range, so ’Length is actually 101. Range This funnily enough returns in this case the value we gave it, but you will see when we come onto arrays how useful this feature is. As you can see these have no direct C/C++ equivalent and are part of the reason for Ada’s reputation for safety, you can define for a parameter exactly the range of values it might take, it all amounts to better practice for large developments where your interface is read by many people who may not be able to tell that the integer parameter day starts at 0, which indicates Wednesday etc. 1.1.6 Arrays {3.6}. Arrays in Ada make use of the range syntax to define their bounds and can be arrays of any type, and can even be declared as unknown size. Some example: char name[31]; int track[3]; int dbla[3][10]; int init[3] = { 0, 1, 2 }; typedef char[31] name_type; track[2] = 1; dbla[0][3] = 2; Name : array (0 .. 30) of Character; -- OR Name : String (1 .. 30); Track : array (0 .. 2) of Integer; DblA : array (0 .. 2) of array (0 .. 9) of Integer; -- OR DblA : array (0 .. 2,0 .. 9) of Integer; : array (0 .. 2) of Integer := (0, 1, 2); Init type Name_Type is array (0 .. 30) of Character; track(2) := 1; dbla(0,3) := 2; -- Note try this in C. a, b : Name_Type; a := b; -- will copy all elements of b into a. Simple isn’t it, you can convert C arrays into Ada arrays very easily. What you don’t get is all the things you can do with Ada arrays that you can’t do in C/C++. 13 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. non-zero based ranges. Because Ada uses ranges to specify the bounds of an array then you can easily set the lower bound to anything you want, for example: Example : array (-10 .. 10) of Integer; non-integer ranges. In the examples above we have used the common abbreviation for range spec- ifiers. The ranges above are all integer ranges, and so we did not need to use the correct form which is: array(type range low .. high) which would make Example above array(Integer range -10 .. where we’re going, take an enumerated type, All_Days and you can define an array: 10). Now you can see Hours_Worked : array (All_Days range Monday .. Friday); unbounded array types. The examples above did demonstrate how to declare an array type. One of Ada’s goals is reuse, and to have to define a function to deal with a 1..10 array, and another for a 0..1000 array is silly. Therefore Ada allows you to define unbounded array types. An unbounded type can be used as a parameter type, but you cannot simply define a variable of such a type. Consider: Vector is array (Integer range <>) of Float; type procedure sort_vector(sort_this : in out Vector); Illegal_Variable : Vector; Legal_Variable subtype SmallVector is Vector(0..1); Another_Legal : Vector(1..5); : SmallVector; This does allow us great flexibility to define functions and procedures to work on arrays regardless of their size, so a call to sort_vector could take the Legal_Variable object or an object of type SmallVector, etc. Note that a variable of type Smallvector is constrained and so can be legally created. array range attributes. If you are passed a type which is an unbounded array then if you want to loop through it then you need to know where it starts. So we can use the range attributes introduced in 1.1.5 to iterate over a given array thus: attributes for array types. Consider: Example : array (1 .. 10) of Integer; for i in Example’First .. Example’Last loop for i in Example’Range loop Note that if you have a multiple dimension array then the above notation implies that the returned values are for the first dimension, use the notation Array_Name’attribute(dimension) for multi-dimensional arrays. 14 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. Initialisation by range (Aggregates {}???) When initialising an array one can initialise a range of elements in one go: Init : array (0 .. 3) of Integer := (0 .. 3 => 1); Init : array (0 .. 3) of Integer := (0 => 1, others => 0); The keyword others sets any elements not explicitly handled. Slicing Array slicing is something usually done with memcpy in C/C++. Take a section out of one array and assign it into another. Large : array (0 .. 100) of Integer; Small : array (0 .. 3) of Integer; -- extract section from one array into another. Small(0 .. 3) := Large(10 .. 13); -- swap top and bottom halfs of an array. Large := Large(51 .. 100) & Large(1..50); Note: Both sides of the assignment must be of the same type, that is the same dimensions with each element the same. The following is illegal. -- extract section from one array into another. Small(0 .. 3) := Large(10 .. 33); -- ^^^^^^^^ range too big. 1.1.7 Records {3.8}. You shouldn’t have too much problem here, you can see an almost direct mapping from C/C++ to Ada for simple structures. Note the example below does not try to convert type to type, thus the C char*, to hold a string is converted to the Ada String type. struct _device { int major_number; int minor_number; char name[20]; }; typedef struct _device Device; type struct_device is record major_number : Integer; minor_number : Integer; name : String(1 .. 19); end record; type Device is new struct_device; 15 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. As you can see, the main difference is that the name we declare for the initial record is a type, and can be used from that point on. In C all we have declared is a structure name, we then require the additional step of typedef-ing to add a new type name. Ada uses the same element reference syntax as C, so to access the minor_number element of an object lp1 of type Device we write lp1.minor_number. Ada does allow, like C, the initialisation of record members at declaration. In the code below we introduce a feature of Ada, the ability to name the elements we are going to initialise. This is useful for clarity of code, but more importantly it allows us to only initialise the bits we want. Device lp1 = {1, 2, "lp1"}; lp1 : Device := (1, 2, "lp1"); lp2 : Device := (major_number => 1, minor_number => 3, name => "lp2"); tmp : Device := (major_number => 255, name => "tmp"); When initialising a record we use an aggregate, a construct which groups together the members. This facility (unlike aggregates in C) can also be used to assign members at other times as well. tmp : Device; -- some processing tmp := (major_number => 255, name => "tmp"); This syntax can be used anywhere where parameters are passed, initialisation (as above) function/procedure calls, variants and discriminants and generics. The code above is most useful if we have a default value for minor_number, so the fact that we left it out won’t matter. This is possible in Ada. This facility improves readability and as far as most Ada programmers believe maintainability. type struct_device is record major_number : Integer := 0; minor_number : Integer := 0; name : String(1 .. 19) := "unknown"; end record; Structures/records like this are simple, and there isn’t much more to say. The more interesting problem for Ada is modelling C unions (see section 1.1.10 on page 22). 1.1.8 Access types (pointers) {3.10}. The topic of pointers/references/access types is the most difficult, each language has its own set of rules and tricks. In C/C++ the thing you must always remember is that the value of a pointer is the real memory address, in Ada it is not. It is a type used to access the data. Ada access types are safer, and in some ways easier to use and understand, but they do mean that a lot of C code which uses pointers heavily will have to be reworked to use some other means. The most common use of access types is in dynamic programming, for example in linked lists. 16 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. struct _device_event { int major_number; int minor_number; int event_ident; struct _device_event* next; }; type Device_Event; type Device_Event_Access is access Device_Event; type Device_Event is record major_number : Integer := 0; minor_number : Integer := 0; event_ident : Integer := 0; next : Device_Event_Access := null; -- Note: the assignement to null is not required, -- Ada automatically initialises access types to -- null if no other value is specified. end record; The Ada code may look long-winded but it is also more expressive, the access type is declared before the record so a real type can be used for the declaration of the element next. Note: we have to forward declare the record before we can declare the access type, is this extra line worth all the moans we hear from the C/C++ community that Ada is overly verbose? When it comes to dynamically allocating a new structure the Ada allocator syntax is much closer to C++ than to C. Event_1 := new Device_Event; Event_1.next := new Device_Event’(1, 2, EV_Paper_Low, null); There are three things of note in the example above. Firstly the syntax, we can say directly that we want a new thing, none of this malloc rubbish. Secondly that there is no difference in syntax between access of elements of a statically allocated record and a dynamically allocated one. We use the record.element syntax for both. Lastly that we can initialise the values as we create the object, the tick is used again, not as an attribute, but with parenthases in order to form a qualified expresssion. Ada allows you to assign between access types, and as you would expect it only changes what the access type points to, not the contents of what it points to. One thing to note again, Ada allows you to assign one structure to another if they are of the same type, and so a syntax is required to assign the contents of an access type, its easier to read than write, so: dev1, dev2 : Device_Event; pdv1, pdv2 : Device_Event_Access; dev1 := dev2; -- all elements copied. pdv1 := pdv2; -- pdv1 now points to contents of pdv2. pdv1.all := pdv2.all; -- !! 17 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. What you may have noticed is that we have not discussed the operator to free the memory we have allocated, the equivalent of C’s free() or C++’s delete. There is a good reason for this, Ada does not have one. To digress for a while, Ada was designed as a language to support garbage collection, that is the runtime would manage deallocation of no longer required dynamic memory. However at that time garbage collection was slow, required a large overhead in tracking dynamic memory and tended to make programs irratic in performance, slowing as the garbage collector kicks in. The language spec- ification therefore states {13.11} "An implementation need not support garbage collection ...". This means that you must, as in C++ manage your own memory deallocation. Ada requires you to use the generic procedure Unchecked_Deallocation (see 1.3.3 on page 36) to deallocate a dynamic object. This procedure must be instantiated for each dynamic type and should not (ideally) be declared on a public package spec, ie provide the client with a deallocation procedure which uses Unchecked_Deallocation internally. 1.1.9 Ada advanced types and tricks. Casting (wow) As you might expect from what we have seen so far Ada must allow us some way to relax the strong typing it enforces. In C the cast allows us to make anything look like something else, in Ada type coersion can allow you to convert between two similar types, ie: type Thing is new Integer; an_Integer : Integer; a_Thing : Thing; an_Integer := a_Thing; -- illegal an_Integer := Integer(a_Thing); This can only be done between similar types, the compiler will not allow such coersion between very different types, for this you need the generic procedure Unchecked_Conversion (see 1.3.3 on page 35) which takes as an argument one type, and returns another. The only constraint on this is that they must be the same size. Procedure types. {} Ada-83 did not allow the passing of procedures as subprogram parameters at execution time, or storing procedures in records etc. The rationale for this was that it broke the ability to statically prove the code. Ada-95 has introduced the ability to define types which are in effect similar to C’s ability to define pointers to functions. In C/C++ there is the most formidable syntax for defining pointers to functions and so the Ada syntax should come as a nice surprise: typedef int (*callback_func)(int param1, int param2); type Callback_Func is access function(param_1 : in Integer; param_2 : in Integer) return Integer; 18 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. Discriminant types {3.7}. Discriminant types are a way of parameterising a compound type (such as a record, tagged, task or protected type). For example: type Event_Item is record Event_ID Event_Info : String(1 .. 80); : Integer; end record; type Event_Log(Max_Size : Integer) is record Log_Opened : Date_Type; Events : array (1 .. Max_Size) of Event_Item; end record; First we declare a type to hold our event information in. We then declare a type which is a log of such events, this log has a maximum size, and rather than the C answer, define an array large enough for the maximum ever, or resort to dynamic programming the Ada approach is to instantiate the record with a max value and at time of instantiation define the size of the array. My_Event_Log : Event_Log(1000); If it is known that nearly all event logs are going to be a thousand items in size, then you could make that a default value, so that the following code is identical to that above. type Event_Log(Max_Size : Integer := 1000) is record Log_Opened : Date_Type Events : array (Integer range 1 .. Max_Size) of Event_Item; end record; My_Event_Log : Event_Log; Again this is another way in which Ada helps, when defining an interface, to state precisely what we want to provide. Variant records {3.8.1}. Anyone who has worked in a Pascal language will recognise variant records, they are a bit like C/C++ unions except that the are very different :-) Ada variant records allow you to define a record which has 2 or more blocks of data of which only one is visible at any time. The visibility of the block is determined by a discriminant which is then ’cased’. type Transport_Type is (Sports, Family, Van); type Car(Type : Transport_Type) is record Registration_Date : Date_Type; 19 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. Colour : Colour_Type; case Type is when Sports => Soft_Top : Boolean; when Family => Number_Seats : Integer; Rear_Belts : Boolean; when Van => Cargo_Capacity: Integer; end case; end record; So if you code My_Car : Car(Family); then you can ask for the number of seats in the car, and whether the car has seat belts in the rear, but you cannot ask if it is a soft top, or what its cargo capacity is. I guess you’ve seen the difference between this and C unions. In a C union representation of the above any block is visible regardless of what type of car it is, you can easily ask for the cargo capacity of a sports car and C will use the bit pattern of the boolean to provide you with the cargo capacity. Not good. To simplify things you can subtype the variant record with types which define the variant (note in the example the use of the designator for clarity). subtype Sports_Car is Car(Sports); subtype Family_Car is Car(Type => Family); subtype Small_Van is Car(Type => Van); Exceptions {11.1}. Exceptions are a feature which C++ is only now getting to grips with, although Ada was designed with exceptions included from the beginning. This does mean that Ada code will use exceptions more often than not, and certainly the standard library packages will raise a number of possible exceptions. Unlike C++ where an exception is identified by its type in Ada they are uniquely identified by name. To define an exception for use, simply parameter_out_of_range : Exception; These look and feel like constants, you cannot assign to them etc, you can only raise an exception and handle an exception. Exceptions can be argued to be a vital part of the safety of Ada code, they cannot easily be ignored, and can halt a system quickly if something goes wrong, far faster than a returned error code which in most cases is completely ignored. System Representation of types {13}. As you might expect with Ada’s background in embedded and systems programming there are ways in which you can force a type into specific system repre- sentations. 20 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. type BYTE is range 0 .. 255; for BYTE use 8; This first example shows the most common form of system representation clause, the size attribute. We have asked the compiler to give us a range, from 0 to 255 and the compiler is at liberty to provide the best type available to hold the representation. We are forcing this type to be 8 bits in size. type DEV_Activity is (READING, WRITING, IDLE); for DEV_Activity use (READING => 1, WRITING => 2, IDLE => 3); Again this is useful for system programming it gives us the safety of enumeration range checking, so we can only put the correct value into a variable, but does allow us to define what the values are if they are being used in a call which expects specific values. type DEV_Available is BYTE; for DEV_Available use at 16#00000340#; This example means that all objects of type DEV_Available are placed at memory address 340 (Hex). This placing of data items can be done on a per object basis by using: type DEV_Available is BYTE; Avail_Flag : DEV_Available; for Avail_Flag’Address use 16#00000340#; Note the address used Ada’s version of the C 0x340 notation, however the general form is base#number# where the base can be anything, including 2, so bit masks are real easy to define, for example: Is_Available : constant BYTE := 2#1000_0000#; Not_Available: constant BYTE := 2#0000_0000#; Another feature of Ada is that any underscores in numeric constants are ignored, so you can break apart large numbers for readability. type DEV_Status is 0 .. 15; type DeviceDetails is record status : DEV_Activity; rd_stat: DEV_Status; wr_stat: DEV_Status; end record; for DeviceDetails use record at mod 2; status at 0 range 0 .. 7; rd_stat at 1 range 0 .. 3; wr_stat at 1 range 4 .. 7; end record; 21 1.1. C/C++ TYPES TO ADA TYPES. 1. ADA BASICS. This last example is the most complex, it defines a simple range type, and a structure. It then defines two things to the compiler, first the mod clause sets the byte packing for the structure, in this case back on two-byte boundaries. The second part of this structure defines exactly the memory image of the record and where each element occurs. The number after the ’at’ is the byte offset and the range, or size, is specified in number of bits. From this you can see that the whole structure is stored in two bytes where the first byte is stored as expected, but the second and third elements of the record share the second byte, low nibble and high nibble. This form becomes very important a little later on. 1.1.10 C Unions in Ada, (food for thought). Ada has more than one way in which it can represent a union as defined in a C program, the method you choose depends on the meaning and usage of the C union. Firstly we must look at the two ways unions are identified. Unions are used to represent the data in memory in more than one way, the programmer must know which way is relevant at any point in time. This variant identifier can be inside the union or outside, for example: struct _device_input { int device_id; union { type_1_data from_type_1; type_2_data from_type_2; } device_data; }; void get_data_func(_device_input* from_device); union device_data { type_1_data from_type_1; type_2_data from_type_2; }; void get_data_func(int *device_id, device_data* from_device); In the first example all the data required is in the structure, we call the function and get back a structure which holds the union and the identifier which denotes which element of the union is active. In the second example only the union is returned and the identifier is seperate. The next step is to decide whether, when converting such code to Ada, you wish to maintain simply the concept of the union, or whether you are required to maintain the memory layout also. Note: the second choice is usually only if your Ada code is to pass such a structure to a C program or get one from it. If you are simply retaining the concept of the union then you would not use the second form, use the first form and use a variant record. type Device_ID is new Integer; type Device_Input(From_Device : Device_ID) is record 22 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. case From_Device is when 1 => From_Type_1 : Type_1_Data; when 2 => From_Type_2 : Type_2_Data; end case; end record; The above code is conceptually the same as the first piece of C code, however it will probably look very different, you could use the following representation clause to make it look like the C code (type sizes are not important). for Device_Input use record From_Device at 0 range 0 .. 15; From_Type_1 at 2 range 0 .. 15; From_Type_2 at 2 range 0 .. 31; end record; You should be able to pass this to and from C code now. You could use a representation clause for the second C case above, but unless you really must pass it to some C code then re-code it as a variant record. We can also use the abilities of Unchecked_Conversion to convert between different types (see 1.3.3 on page 35). This allows us to write the following: type Type_1_Data is record Data_1 : Integer; end record; type Type_2_Data is record Data_1 : Integer; end record; function Type_1_to_2 is new Unchecked_Conversion (Source => Type_1_data, Target => Type_2_Data); This means that we can read/write items of type Type_1_Data and when we need to represent the data as Type_2_Data we can simply write Type_1_Object : Type_1_Data := ReadData; : Type_2_Object : Type_2_Data := Type_1_to_2(Type_1_Object); 1.2 C/C++ statements to Ada. I present below the set of C/C++ statement types available, with each its Ada equivalent. Note: All Ada statements can be qualified by a name, this be discussed further in the section on Ada looping constructs, however it can be used anywhere to improve readability, for example: 23 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. begin Init_Code: begin Some_Code; end Init_Code; Main_Loop: loop if Some_Value then exit loop Main_Loop; end if; end loop Main_Loop; Term_Code: begin Some_Code; end Term_Code; end A_Block; 1.2.1 Compound Statement {5.6} A compound statement is also known as a block and in C allows you to define variables local to that block, in C++ variables can be defined anywhere. In Ada they must be declared as part of the block, but must appear in the declare part just before the block starts. { } declarations statements declare declarations begin statement end; 1.2.2 if Statement {5.3} If statements are the primary selection tool available to programmers. The Ada if statement also has the ’elsif’ construct (which can be used more than once in any if statement), very useful for large complex selections where a switch/case statement is not possible. Note: Ada does not require brackets around the expressions used in if, case or loop statements. if (expression) { statement } else { statement } 24 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. if expression then statement elsif expression then statement else statement end if; 1.2.3 switch Statement {5.4} The switch or case statement is a very useful tool where the number of possible values is large, and the selection expression is of a constant scalar type. switch (expression) { case value: statement default: statement } case expression is => statement when value when others => statement end case; There is a point worth noting here. In C the end of the statement block between case statements is a break statement, otherwise we drop through into the next case. In Ada this does not happen, the end of the statement is the next case. This leads to a slight problem, it is not uncommon to find a switch statement in C which looks like this: switch (integer_value) { case 1: case 2: case 3: case 4: value_ok = 1; break; case 5: case 6: case 7: break; } This uses ranges (see 1.1.5 on page 11) to select a set of values for a single operation, Ada also allows you to or values together, consider the following: 25 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. case integer_value is when 1 .. 4 => value_ok := 1; when 5 | 6 | 7 => null; end case; You will also note that in Ada there must be a statement for each case, so we have to use the Ada null statement as the target of the second selection. 1.2.4 Ada loops {5.5} All Ada loops are built around the simple loop ... end construct loop statement end loop; 1.2.4.1 while Loop The while loop is common in code and has a very direct Ada equivalent. while (expression) { statement } while expression loop statement end loop; 1.2.4.2 do Loop The do loop has no direct Ada equivalent, though section 1.2.4.4 will show you how to synthesize one. do { statement } while (expression) -- no direct Ada equivalent. 1.2.4.3 for Loop The for loop is another favourite, Ada has no direct equivalent to the C/C++ for loop (the most frighteningly overloaded statement in almost any language) but does allow you to iterate over a range, allowing you access to the most common usage of the for loop, iterating over an array. 26 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. for (init-statement ; expression-1 ; loop-statement) { statement } for ident in range loop statement end loop; However Ada adds some nice touches to this simple statement. Firstly, the variable ident is actually declared by its appearance in the loop, it is a new variable which exists for the scope of the loop only and takes the correct type according to the specified range. Secondly you will have noticed that to loop for 1 to 10 you can write the following Ada code: for i in 1 .. 10 loop null; end loop; What if you want to loop from 10 down to 1? In Ada you cannot specify a range of 10 .. 1 as this is defined as a ’null range’. Passing a null range to a for loop causes it to exit immediatly. The code to iterate over a null range such as this is: for i in reverse 1 .. 10 loop null; end loop; 1.2.4.4 break and continue In C and C++ we have two useful statements break and continue which may be used to add fine control to loops. Consider the following C code: while (expression) { if (expression1) { continue; } if (expression2) { break; } } This code shows how break and continue are used, you have a loop which takes an expression to determine general termination procedure. Now let us assume that during execution of the loop you decide that you have completed what you wanted to do and may leave the loop early, the break forces a ’jump’ to the next statement after the closing brace of the loop. A continue is similar but it takes you to the first statement after the opening brace of the loop, in effect it allows you to reevaluate the loop. In Ada there is no continue, and break is now exit. 27 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. while expression loop if expression2 then exit; end if; end loop; The Ada exit statement however can combine the expression used to decide that it is required, and so the code below is often found. while expression loop exit when expression2; end loop; This leads us onto the do loop, which can now be coded as: loop statement exit when expression; end loop; Another useful feature which C and C++ lack is the ability to ’break’ out of nested loops, consider while ((!feof(file_handle) && (!percent_found)) { for (char_index = 0; buffer[char_index] != ’\n’; char_index++) { if (buffer[char_index] == ’%’) { percent_found = 1; break; } // some other code, including get next line. } } This sort of code is quite common, an inner loop spots the termination condition and has to signal this back to the outer loop. Now consider Main_Loop: while not End_Of_File(File_Handle) loop for Char_Index in Buffer’Range loop exit when Buffer(Char_Index) = NEW_LINE; exit Main_Loop when Buffer(Char_Index) = PERCENT; end loop; end loop Main_Loop; 1.2.5 return {6.5} Here again a direct Ada equivalent, you want to return a value, then return a value, return value; // C++ return return value; -- Ada return 28 1.2. C/C++ STATEMENTS TO ADA. 1. ADA BASICS. 1.2.6 labels and goto {5.8} Don’t do it !!, OK one day you might need to, so heres how. Declare a label and jump to it. label: goto label; <