1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- I N T E R F A C E S . C --
-- --
-- S p e c --
-- --
-- This specification is derived from the Ada Reference Manual for use with --
-- GNAT. In accordance with the copyright of that document, you can freely --
-- copy and modify this specification, provided that if you redistribute a --
-- modified version, any changes that you have made are clearly indicated. --
-- --
------------------------------------------------------------------------------
-- Preconditions in this unit are meant for analysis only, not for run-time
-- checking, so that the expected exceptions are raised. This is enforced by
-- setting the corresponding assertion policy to Ignore. Postconditions and
-- contract cases should not be executed at runtime as well, in order not to
-- slow down the execution of these functions.
pragma Assertion_Policy (Pre => Ignore,
Post => Ignore,
Contract_Cases => Ignore,
Ghost => Ignore);
-- Pre/postconditions use a fully qualified name for the standard "+" operator
-- in order to work around an internal limitation of the compiler.
with System;
with System.Parameters;
package Interfaces.C with
SPARK_Mode,
Pure,
Always_Terminates
is
-- Each of the types declared in Interfaces.C is C-compatible.
-- The types int, short, long, unsigned, ptrdiff_t, size_t, double,
-- char, wchar_t, char16_t, and char32_t correspond respectively to the
-- C types having the same names. The types signed_char, unsigned_short,
-- unsigned_long, unsigned_char, C_bool, C_float, and long_double
-- correspond respectively to the C types signed char, unsigned
-- short, unsigned long, unsigned char, bool, float, and long double.
-- Declaration's based on C's <limits.h>
CHAR_BIT : constant := 8;
SCHAR_MIN : constant := -128;
SCHAR_MAX : constant := 127;
UCHAR_MAX : constant := 255;
-- Signed and Unsigned Integers. Note that in GNAT, we have ensured that
-- the standard predefined Ada types correspond to the standard C types
-- Note: the Integer qualifications used in the declaration of type long
-- avoid ambiguities when compiling in the presence of s-auxdec.ads and
-- a non-private system.address type.
type int is new Integer;
type short is new Short_Integer;
type long is range -(2 ** (System.Parameters.long_bits - Integer'(1)))
.. +(2 ** (System.Parameters.long_bits - Integer'(1))) - 1;
type long_long is new Long_Long_Integer;
type signed_char is range SCHAR_MIN .. SCHAR_MAX;
for signed_char'Size use CHAR_BIT;
type unsigned is mod 2 ** int'Size;
type unsigned_short is mod 2 ** short'Size;
type unsigned_long is mod 2 ** long'Size;
type unsigned_long_long is mod 2 ** long_long'Size;
type unsigned_char is mod (UCHAR_MAX + 1);
for unsigned_char'Size use CHAR_BIT;
-- Note: Ada RM states that the type of the subtype plain_char is either
-- signed_char or unsigned_char, depending on the C implementation. GNAT
-- instead choses unsigned_char always.
subtype plain_char is unsigned_char;
-- Note: the Integer qualifications used in the declaration of ptrdiff_t
-- avoid ambiguities when compiling in the presence of s-auxdec.ads and
-- a non-private system.address type.
type ptrdiff_t is
range -System.Memory_Size / 2 .. System.Memory_Size / 2 - 1;
type size_t is mod System.Memory_Size;
-- Boolean type
type C_bool is new Boolean;
pragma Convention (C, C_bool);
-- Floating-Point
type C_float is new Float;
type double is new Standard.Long_Float;
type long_double is new Standard.Long_Long_Float;
----------------------------
-- Characters and Strings --
----------------------------
type char is new Character;
nul : constant char := char'First;
-- The functions To_C and To_Ada map between the Ada type Character and the
-- C type char.
function To_C (Item : Character) return char
with
Post => To_C'Result = char'Val (Character'Pos (Item));
function To_Ada (Item : char) return Character
with
Post => To_Ada'Result = Character'Val (char'Pos (Item));
type char_array is array (size_t range <>) of aliased char;
for char_array'Component_Size use CHAR_BIT;
function Is_Nul_Terminated (Item : char_array) return Boolean
with
Post => Is_Nul_Terminated'Result = (for some C of Item => C = nul);
-- The result of Is_Nul_Terminated is True if Item contains nul, and is
-- False otherwise.
function C_Length_Ghost (Item : char_array) return size_t
with
Ghost,
Pre => Is_Nul_Terminated (Item),
Post => C_Length_Ghost'Result <= Item'Last - Item'First
and then Item (Item'First + C_Length_Ghost'Result) = nul
and then (for all J in Item'First .. Item'First + C_Length_Ghost'Result
when J /= Item'First + C_Length_Ghost'Result =>
Item (J) /= nul);
-- Ghost function to compute the length of a char_array up to the first nul
-- character.
function To_C
(Item : String;
Append_Nul : Boolean := True) return char_array
with
Pre => not (Append_Nul = False and then Item'Length = 0),
Post => To_C'Result'First = 0
and then To_C'Result'Length =
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length)
and then (for all J in Item'Range =>
To_C'Result (size_t (J - Item'First)) = To_C (Item (J)))
and then (if Append_Nul then To_C'Result (To_C'Result'Last) = nul);
-- The result of To_C is a char_array value of length Item'Length (if
-- Append_Nul is False) or Item'Length+1 (if Append_Nul is True). The lower
-- bound is 0. For each component Item(I), the corresponding component
-- in the result is To_C applied to Item(I). The value nul is appended if
-- Append_Nul is True. If Append_Nul is False and Item'Length is 0, then
-- To_C propagates Constraint_Error.
function To_Ada
(Item : char_array;
Trim_Nul : Boolean := True) return String
with
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Natural'Last)
else
Item'Last - Item'First < size_t (Natural'Last)),
Post => To_Ada'Result'First = 1
and then To_Ada'Result'Length =
(if Trim_Nul then C_Length_Ghost (Item) else Item'Length)
and then (for all J in To_Ada'Result'Range =>
To_Ada'Result (J) =
To_Ada (Item (size_t (J) - 1 + Item'First)));
-- The result of To_Ada is a String whose length is Item'Length (if
-- Trim_Nul is False) or the length of the slice of Item preceding the
-- first nul (if Trim_Nul is True). The lower bound of the result is 1.
-- If Trim_Nul is False, then for each component Item(I) the corresponding
-- component in the result is To_Ada applied to Item(I). If Trim_Nul
-- is True, then for each component Item(I) before the first nul the
-- corresponding component in the result is To_Ada applied to Item(I). The
-- function propagates Terminator_Error if Trim_Nul is True and Item does
-- not contain nul.
procedure To_C
(Item : String;
Target : out char_array;
Count : out size_t;
Append_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => Target'Length >=
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length),
Post => Count = (if Append_Nul then Item'Length + 1 else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Item'Range =>
Target (Target'First + size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then Target (Target'First + (Count - 1)) = nul);
-- For procedure To_C, each element of Item is converted (via the To_C
-- function) to a char, which is assigned to the corresponding element of
-- Target. If Append_Nul is True, nul is then assigned to the next element
-- of Target. In either case, Count is set to the number of Target elements
-- assigned. If Target is not long enough, Constraint_Error is propagated.
procedure To_Ada
(Item : char_array;
Target : out String;
Count : out Natural;
Trim_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Target'Length)
else
Item'Last - Item'First < size_t (Target'Length)),
Post => Count =
(if Trim_Nul then Natural (C_Length_Ghost (Item)) else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Target'First .. Target'First + (Count - 1) =>
Target (J) =
To_Ada (Item (size_t (J - Target'First) + Item'First)));
-- For procedure To_Ada, each element of Item (if Trim_Nul is False) or
-- each element of Item preceding the first nul (if Trim_Nul is True) is
-- converted (via the To_Ada function) to a Character, which is assigned
-- to the corresponding element of Target. Count is set to the number of
-- Target elements assigned. If Target is not long enough, Constraint_Error
-- is propagated. If Trim_Nul is True and Item does not contain nul, then
-- Terminator_Error is propagated.
------------------------------------
-- Wide Character and Wide String --
------------------------------------
type wchar_t is new Wide_Character;
for wchar_t'Size use Standard'Wchar_T_Size;
wide_nul : constant wchar_t := wchar_t'First;
-- To_C and To_Ada provide the mappings between the Ada and C wide
-- character types.
function To_C (Item : Wide_Character) return wchar_t
with
Post => To_C'Result = wchar_t (Item);
function To_Ada (Item : wchar_t) return Wide_Character
with
Post => To_Ada'Result = Wide_Character (Item);
type wchar_array is array (size_t range <>) of aliased wchar_t;
function Is_Nul_Terminated (Item : wchar_array) return Boolean
with
Post => Is_Nul_Terminated'Result = (for some C of Item => C = wide_nul);
-- The result of Is_Nul_Terminated is True if Item contains wide_nul, and
-- is False otherwise.
-- The To_C and To_Ada subprograms that convert between Wide_String and
-- wchar_array have analogous effects to the To_C and To_Ada subprograms
-- that convert between String and char_array, except that wide_nul is
-- used instead of nul.
function C_Length_Ghost (Item : wchar_array) return size_t
with
Ghost,
Pre => Is_Nul_Terminated (Item),
Post => C_Length_Ghost'Result <= Item'Last - Item'First
and then Item (Item'First + C_Length_Ghost'Result) = wide_nul
and then (for all J in Item'First .. Item'First + C_Length_Ghost'Result
when J /= Item'First + C_Length_Ghost'Result =>
Item (J) /= wide_nul);
-- Ghost function to compute the length of a wchar_array up to the first
-- wide_nul character.
function To_C
(Item : Wide_String;
Append_Nul : Boolean := True) return wchar_array
with
Pre => not (Append_Nul = False and then Item'Length = 0),
Post => To_C'Result'First = 0
and then To_C'Result'Length =
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length)
and then (for all J in Item'Range =>
To_C'Result (size_t (J - Item'First)) = To_C (Item (J)))
and then (if Append_Nul then To_C'Result (To_C'Result'Last) = wide_nul);
function To_Ada
(Item : wchar_array;
Trim_Nul : Boolean := True) return Wide_String
with
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Natural'Last)
else
Item'Last - Item'First < size_t (Natural'Last)),
Post => To_Ada'Result'First = 1
and then To_Ada'Result'Length =
(if Trim_Nul then C_Length_Ghost (Item) else Item'Length)
and then (for all J in To_Ada'Result'Range =>
To_Ada'Result (J) =
To_Ada (Item (size_t (J) - 1 + Item'First)));
procedure To_C
(Item : Wide_String;
Target : out wchar_array;
Count : out size_t;
Append_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => Target'Length >=
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length),
Post => Count = (if Append_Nul then Item'Length + 1 else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Item'Range =>
Target (Target'First + size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then Target (Target'First + (Count - 1)) = wide_nul);
procedure To_Ada
(Item : wchar_array;
Target : out Wide_String;
Count : out Natural;
Trim_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Target'Length)
else
Item'Last - Item'First < size_t (Target'Length)),
Post => Count =
(if Trim_Nul then Natural (C_Length_Ghost (Item)) else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Target'First .. Target'First + (Count - 1) =>
Target (J) =
To_Ada (Item (size_t (J - Target'First) + Item'First)));
Terminator_Error : exception;
-- The remaining declarations are for Ada 2005 (AI-285)
-- ISO/IEC 10646:2003 compatible types defined by SC22/WG14 document N1010
type char16_t is new Wide_Character;
pragma Ada_05 (char16_t);
char16_nul : constant char16_t := char16_t'Val (0);
pragma Ada_05 (char16_nul);
-- To_C and To_Ada provide mappings between the Ada and C 16-bit character
-- types.
function To_C (Item : Wide_Character) return char16_t
with
Post => To_C'Result = char16_t (Item);
pragma Ada_05 (To_C);
function To_Ada (Item : char16_t) return Wide_Character
with
Post => To_Ada'Result = Wide_Character (Item);
pragma Ada_05 (To_Ada);
type char16_array is array (size_t range <>) of aliased char16_t;
pragma Ada_05 (char16_array);
function Is_Nul_Terminated (Item : char16_array) return Boolean
with
Post => Is_Nul_Terminated'Result = (for some C of Item => C = char16_nul);
pragma Ada_05 (Is_Nul_Terminated);
-- The result of Is_Nul_Terminated is True if Item contains char16_nul, and
-- is False otherwise.
-- The To_C and To_Ada subprograms that convert between Wide_String and
-- char16_array have analogous effects to the To_C and To_Ada subprograms
-- that convert between String and char_array, except that char16_nul is
-- used instead of nul.
function C_Length_Ghost (Item : char16_array) return size_t
with
Ghost,
Pre => Is_Nul_Terminated (Item),
Post => C_Length_Ghost'Result <= Item'Last - Item'First
and then Item (Item'First + C_Length_Ghost'Result) = char16_nul
and then (for all J in Item'First .. Item'First + C_Length_Ghost'Result
when J /= Item'First + C_Length_Ghost'Result =>
Item (J) /= char16_nul);
-- Ghost function to compute the length of a char16_array up to the first
-- char16_nul character.
function To_C
(Item : Wide_String;
Append_Nul : Boolean := True) return char16_array
with
Pre => not (Append_Nul = False and then Item'Length = 0),
Post => To_C'Result'First = 0
and then To_C'Result'Length =
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length)
and then (for all J in Item'Range =>
To_C'Result (size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then To_C'Result (To_C'Result'Last) = char16_nul);
pragma Ada_05 (To_C);
function To_Ada
(Item : char16_array;
Trim_Nul : Boolean := True) return Wide_String
with
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Natural'Last)
else
Item'Last - Item'First < size_t (Natural'Last)),
Post => To_Ada'Result'First = 1
and then To_Ada'Result'Length =
(if Trim_Nul then C_Length_Ghost (Item) else Item'Length)
and then (for all J in To_Ada'Result'Range =>
To_Ada'Result (J) =
To_Ada (Item (size_t (J) - 1 + Item'First)));
pragma Ada_05 (To_Ada);
procedure To_C
(Item : Wide_String;
Target : out char16_array;
Count : out size_t;
Append_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => Target'Length >=
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length),
Post => Count = (if Append_Nul then Item'Length + 1 else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Item'Range =>
Target (Target'First + size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then Target (Target'First + (Count - 1)) = char16_nul);
pragma Ada_05 (To_C);
procedure To_Ada
(Item : char16_array;
Target : out Wide_String;
Count : out Natural;
Trim_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Target'Length)
else
Item'Last - Item'First < size_t (Target'Length)),
Post => Count =
(if Trim_Nul then Natural (C_Length_Ghost (Item)) else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Target'First .. Target'First + (Count - 1) =>
Target (J) =
To_Ada (Item (size_t (J - Target'First) + Item'First)));
pragma Ada_05 (To_Ada);
type char32_t is new Wide_Wide_Character;
pragma Ada_05 (char32_t);
char32_nul : constant char32_t := char32_t'Val (0);
pragma Ada_05 (char32_nul);
-- To_C and To_Ada provide mappings between the Ada and C 32-bit character
-- types.
function To_C (Item : Wide_Wide_Character) return char32_t
with
Post => To_C'Result = char32_t (Item);
pragma Ada_05 (To_C);
function To_Ada (Item : char32_t) return Wide_Wide_Character
with
Post => To_Ada'Result = Wide_Wide_Character (Item);
pragma Ada_05 (To_Ada);
type char32_array is array (size_t range <>) of aliased char32_t;
pragma Ada_05 (char32_array);
function Is_Nul_Terminated (Item : char32_array) return Boolean
with
Post => Is_Nul_Terminated'Result = (for some C of Item => C = char32_nul);
pragma Ada_05 (Is_Nul_Terminated);
-- The result of Is_Nul_Terminated is True if Item contains char32_nul, and
-- is False otherwise.
function C_Length_Ghost (Item : char32_array) return size_t
with
Ghost,
Pre => Is_Nul_Terminated (Item),
Post => C_Length_Ghost'Result <= Item'Last - Item'First
and then Item (Item'First + C_Length_Ghost'Result) = char32_nul
and then (for all J in Item'First .. Item'First + C_Length_Ghost'Result
when J /= Item'First + C_Length_Ghost'Result =>
Item (J) /= char32_nul);
-- Ghost function to compute the length of a char32_array up to the first
-- char32_nul character.
-- The To_C and To_Ada subprograms that convert between Wide_Wide_String
-- and char32_array have analogous effects to the To_C and To_Ada
-- subprograms that convert between String and char_array, except
-- that char32_nul is used instead of nul.
function To_C
(Item : Wide_Wide_String;
Append_Nul : Boolean := True) return char32_array
with
Pre => not (Append_Nul = False and then Item'Length = 0),
Post => To_C'Result'First = 0
and then To_C'Result'Length =
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length)
and then (for all J in Item'Range =>
To_C'Result (size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then To_C'Result (To_C'Result'Last) = char32_nul);
pragma Ada_05 (To_C);
function To_Ada
(Item : char32_array;
Trim_Nul : Boolean := True) return Wide_Wide_String
with
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Natural'Last)
else
Item'Last - Item'First < size_t (Natural'Last)),
Post => To_Ada'Result'First = 1
and then To_Ada'Result'Length =
(if Trim_Nul then C_Length_Ghost (Item) else Item'Length)
and then (for all J in To_Ada'Result'Range =>
To_Ada'Result (J) =
To_Ada (Item (size_t (J) - 1 + Item'First)));
pragma Ada_05 (To_Ada);
procedure To_C
(Item : Wide_Wide_String;
Target : out char32_array;
Count : out size_t;
Append_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => Target'Length >=
(if Append_Nul then Standard."+" (Item'Length, 1) else Item'Length),
Post => Count = (if Append_Nul then Item'Length + 1 else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Item'Range =>
Target (Target'First + size_t (J - Item'First)) = To_C (Item (J)))
and then
(if Append_Nul then Target (Target'First + (Count - 1)) = char32_nul);
pragma Ada_05 (To_C);
procedure To_Ada
(Item : char32_array;
Target : out Wide_Wide_String;
Count : out Natural;
Trim_Nul : Boolean := True)
with
Relaxed_Initialization => Target,
Pre => (if Trim_Nul then
Is_Nul_Terminated (Item)
and then C_Length_Ghost (Item) <= size_t (Target'Length)
else
Item'Last - Item'First < size_t (Target'Length)),
Post => Count =
(if Trim_Nul then Natural (C_Length_Ghost (Item)) else Item'Length)
and then
(if Count /= 0 then
Target (Target'First .. Target'First + (Count - 1))'Initialized)
and then
(for all J in Target'First .. Target'First + (Count - 1) =>
Target (J) =
To_Ada (Item (size_t (J - Target'First) + Item'First)));
pragma Ada_05 (To_Ada);
end Interfaces.C;
|