-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathunits.c
972 lines (803 loc) · 20.2 KB
/
units.c
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
#include "c2ada.h"
#include "hostinfo.h"
extern int auto_package;
extern int ada_version;
#undef NULL
#define NULL 0
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#ifndef MAX
#define MAX_NEST 64
#endif
/* unit_t is not externally visible */
#define BITS_PER_INT (sizeof(int) * BITS_PER_BYTE)
#define UNIT_SET_SIZE ((MAX_UNIQ_FNAMES + BITS_PER_INT - 1) / BITS_PER_INT)
typedef int unit_ref_t[UNIT_SET_SIZE];
typedef struct
{
unsigned initialized : 1;
unsigned unit_has_ellipsis : 1;
unsigned unit_has_const_string : 1;
unsigned unit_has_unchecked_conversion : 1;
unsigned unchecked_conversions_in_spec : 1;
unsigned unit_with_c_pointers : 1;
unsigned unit_with_c_const_pointers : 1;
unsigned unit_has_private_part : 1;
unsigned header_comment_set : 1;
char* src_path; /* C source file name */
char* h_src_path;
char* c_src_path;
file_id_t h_file_id;
file_id_t c_file_id;
char* unit_name; /* Ada package name */
char* unit_path; /* Output spec file name */
char* unit_body_path; /* Output body file name */
unit_ref_t unit_ref; /* implicit dependencies */
unit_ref_t from_body; /* implicit references from body */
unit_ref_t direct_ref; /* explicit includes */
comment_block_pt header_comment; /* comment at beginning */
comment_block_pt trailer_comment; /* comment at unit end */
} unit_t;
/* OUTPUT FILE DESCRIPTORS */
FILE* cur_unit_fd; /* set to cur_spec_fd or cur_body_fd as appropriate */
FILE* cur_spec_fd; /* current spec output file */
FILE* cur_body_fd; /* current body output file */
char* predef_pkg = "C"; /* This may be overwritten by a command line switch*/
static unit_t* table[MAX_UNIQ_FNAMES];
static unit_n cur_unit;
/* <file_unit_map> keeps track of file-unit
* (file_id_t -> unit_n) correspondences.
* The initialization is a special signal that the whole table
* hasn't been initialized.
* <unit_count> is the number of active entries in <unit_map>.
* These values are maintained in <file_unit>.
*/
static unit_n file_unit_map[MAX_UNIQ_FNAMES] = { -2 };
static int unit_count = 0;
static int nesting_table[MAX_NEST];
static int nest_level;
void format_to_spec();
bool unchecked_conversions_to_spec(unit_n unit);
void output_to_spec(void);
void format_to_body();
unit_n current_unit(void)
{
if(!auto_package)
return 0;
assert(cur_unit < unit_count);
return cur_unit;
}
bool is_current_unit(unit_n unit)
{
return (cur_unit == unit);
}
static void set_reference(unit_ref_t ref, unit_n ord)
{
int index;
int bit;
index = ord / BITS_PER_INT;
assert(index < UNIT_SET_SIZE);
bit = 1 << (ord % BITS_PER_INT);
ref[index] |= bit;
}
static void clear_reference(unit_ref_t ref, unit_n ord)
{
int index;
int bit;
index = ord / BITS_PER_INT;
assert(index < UNIT_SET_SIZE);
bit = 1 << (ord % BITS_PER_INT);
ref[index] &= ~bit;
}
static bool valid_unit(unit_n unit)
{
return unit >= 0 && unit < MAX_UNIQ_FNAMES && table[unit] != NULL;
}
void set_ellipsis(unit_n unit)
{
assert(valid_unit(unit));
table[unit]->unit_has_ellipsis = true;
}
bool has_ellipsis(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unit_has_ellipsis;
}
void set_unit_has_private_part(unit_n unit)
{
assert(valid_unit(unit));
table[unit]->unit_has_private_part = true;
}
bool unit_has_private_part(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unit_has_private_part;
}
void set_unchecked_conversion(unit_n unit, bool in_spec)
{
assert(valid_unit(unit));
table[unit]->unit_has_unchecked_conversion = true;
table[unit]->unchecked_conversions_in_spec = in_spec;
}
bool has_unchecked_conversion(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unit_has_unchecked_conversion;
}
bool unchecked_conversions_to_spec(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unchecked_conversions_in_spec
|| configured_source_flag(table[unit]->src_path,
"unchecked_conversions_to_spec", false);
}
void with_c_pointers(unit_n unit)
{
assert(valid_unit(unit));
table[unit]->unit_with_c_pointers = true;
}
void with_c_const_pointers(unit_n unit)
{
assert(valid_unit(unit));
table[unit]->unit_with_c_const_pointers = true;
}
bool has_c_pointers(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unit_with_c_pointers;
}
bool has_c_const_pointers(unit_n unit)
{
assert(valid_unit(unit));
return table[unit]->unit_with_c_const_pointers;
}
static bool is_referenced(unit_ref_t ref, unit_n ord)
/* Is ord in the set ref? */
{
int index;
int bit;
index = ord / BITS_PER_INT;
assert(index < UNIT_SET_SIZE);
bit = 1 << (ord % BITS_PER_INT);
return (ref[index] & bit) != 0;
}
static void ref_merge(unit_ref_t r1, unit_ref_t r2)
/* r1 := union(r1, r2) */
{
int i;
for(i = 0; i < UNIT_SET_SIZE; i++)
{
r1[i] |= r2[i];
}
}
static void merge_direct_refs(void)
/*
* Calculates the closure of unit references for all
* units.
* TBD: Why would be want to do this??
*/
{
unit_n last = unit_count;
unit_n i, j;
if(!auto_package)
return;
for(i = 0; i < last; i++)
{
unit_t* unit_i;
unit_t* ref_j;
unit_i = table[i];
assert(unit_i != NULL);
for(j = 0; j < last; j++)
{
if(j != i && is_referenced(unit_i->direct_ref, j))
{
ref_j = table[j];
assert(ref_j != NULL);
ref_merge(unit_i->direct_ref, ref_j->direct_ref);
}
}
/* A unit shouldn't "with" itself */
clear_reference(unit_i->direct_ref, i);
clear_reference(unit_i->unit_ref, i);
}
}
void unit_start_gen(void)
{
cur_unit_fd = cur_spec_fd = cur_body_fd = stdout;
merge_direct_refs();
}
static char* gen_unit_name(char* path)
{
char buf[PATH_MAX];
char res[PATH_MAX];
char* p;
int prefix_len;
if((prefix_len = in_system_search_path(path)))
{
p = buf;
path += prefix_len;
}
else
{
if(ada_version < 1995)
{
char* last = path;
char* s;
for(s = path; *s; s++)
{
if(*s == '/')
{
last = s;
}
}
path = last;
}
p = buf;
}
for(; *path; path++)
{
switch(*path)
{
case '.':
if((path[1] == 'h') && (path[2] == '\0'))
{
/* convert "header.h" into "header", not "header_h" */
goto after_loop;
}
else if((path[1] == 'c') && (path[2] == '\0'))
{
goto after_loop;
}
else
{
*p++ = '_';
}
break;
case '/':
while(path[1] == '/')
path++;
if(ada_version >= 1995)
/* Convert directory structure into child packages */
*p++ = '.';
else
*p++ = '_';
break;
default:
*p++ = *path;
break;
}
}
after_loop:
*p = 0;
make_ada_identifier(buf, res);
return new_string(res);
}
/* An external file may be used to map the names of units, instead
* of using the names that c2ada would otherwise automatically generate.
*/
typedef struct _unit_map
{
char* include_name;
char* package_name;
char* spec_name;
char* body_name;
struct _unit_map* next;
} unit_map;
static char* bindings_dir(void);
static unit_map* decode_unit_map(void)
{
/*
* Map file cbind.map used to map unit names
* This file is expected to be broken into lines
*
* include_file_name package_name spec_file_name body_file_name
*/
char buf1[100], buf2[100], buf3[100], buf4[100], buf[100];
FILE* f = fopen("cbind.map", "r");
unit_map *first, *current;
if(f != NULL)
{
first = NULL;
while(!feof(f))
{
if(first == NULL)
{
first = current = (unit_map*)allocate(sizeof(unit_map));
}
else
{
current->next = (unit_map*)allocate(sizeof(unit_map));
current = current->next;
}
fscanf(f, "%s %s %s %s\n", buf1, buf2, buf3, buf4);
current->include_name = new_string(buf1);
current->package_name = new_string(buf2);
sprintf(buf, "%s/%s", bindings_dir(), buf3);
current->spec_name = new_string(buf);
sprintf(buf, "%s/%s", bindings_dir(), buf4);
current->body_name = new_string(buf);
}
}
return first;
}
static void dump_unit_map(unit_map* first)
/* for debugging */
{
printf("include name\tpackage name\tspec name\tbody name\n");
printf("------------\t------------\t---------\t---------\n");
while(first)
{
printf("%s\t%s\t%s\t%s\n", first->include_name, first->package_name,
first->spec_name, first->body_name);
first = first->next;
}
}
static unit_map* find_unit(unit_map* first, char* iname)
{
while(first)
{
if(!compare_path(first->include_name, iname))
return first;
first = first->next;
}
return NULL;
}
static void initialize_unit(unit_n ord, file_id_t file)
{
static bool first_time = true;
static unit_map* umap = NULL;
char buf[PATH_MAX];
char buf2[PATH_MAX];
unit_t* unit;
char* start_unit;
extern int map_files;
unit_map* this_unit;
assert(table[ord] == NULL);
if(first_time)
{
if(map_files)
umap = decode_unit_map();
first_time = false;
}
unit = NEW(unit_t);
unit->src_path = file_name_from_ord(ord);
assert(unit->src_path != NULL);
if((this_unit = find_unit(umap, unit->src_path)) == NULL)
{
unit->unit_name = gen_unit_name(unit->src_path);
assert(unit->unit_name != NULL);
sprintf(buf, "%s/", bindings_dir());
start_unit = &buf[strlen(buf)];
strcat(buf, unit->unit_name);
switch(ada_compiler)
{
case VADS:
strcpy(buf2, buf);
strcat(buf, ".a");
strcat(buf2, ".bdy.a");
break;
case GNAT:
{
char* p;
char c;
for(p = start_unit; (c = *p) != '\0'; p++)
{
if(c == '.')
{
if(p - start_unit >= 2)
{
*p = '-';
}
else
{
*p = '+';
}
}
}
for(p = buf; (c = *p); p++)
{
if(isupper(c))
*p = tolower(c);
}
strcpy(buf2, buf);
strcat(buf, ".ads");
strcat(buf2, ".adb");
break;
}
break;
case Rational:
strcpy(buf2, buf);
strcat(buf, ".1.ada");
strcat(buf2, ".2.ada");
break;
default:
strcpy(buf2, buf);
strcat(buf, ".ada");
strcat(buf2, ".body.ada");
break;
}
unit->unit_path = new_string(buf);
unit->unit_body_path = new_string(buf2);
}
else
{
unit->unit_name = this_unit->package_name;
unit->unit_path = this_unit->spec_name;
unit->unit_body_path = this_unit->body_name;
}
unit->initialized = true;
table[ord] = unit;
}
static void dump_unit(unit_n ord)
/* print out information about a unit from the unit table */
/* for debugging */
{
unit_t* unit;
unit = table[ord];
assert(unit != NULL);
printf("unit %d\n", ord);
printf("\tsrc = %s\n", unit->src_path);
printf("\tname = %s\n", unit->unit_name);
printf("\tpath = %s\n", unit->unit_path);
fflush(stdout);
}
void unit_included(file_pos_t pos, int nest)
/*
* <pos> indicates a unit <ord> that was found in an #include
* directive. <nest> indicates the depth of the include.
* This routine uses the nesting table to keep track of what's
* included in what. An inclusion results in a "with" clause.
*/
{
unit_t* unit;
int ord;
int uord;
if(!auto_package)
return;
if(nest < 1)
return;
if(nest >= MAX_NEST)
return;
ord = pos_unit(pos);
if(nest > nest_level)
{
uord = nesting_table[nest_level];
unit = table[uord];
assert(unit != NULL);
set_reference(unit->direct_ref, ord);
/* set_reference(unit->unit_ref, ord); */
}
nest_level = nest;
nesting_table[nest] = ord;
}
void init_unit(file_pos_t pos)
{
unit_n unit;
file_id_t file;
if(!auto_package)
return;
unit = pos_unit(pos);
file = pos_file(pos);
if(table[unit] == NULL)
{
initialize_unit(unit, file);
}
cur_unit = unit;
}
static char* bindings_dir()
{
static char* result;
if(!result)
{
result = configured_output_dir();
if(!result)
result = "bindings";
}
return result;
}
bool set_unit(unit_n ord)
/* returns true only if output file can't be opened */
{
unit_t* unit;
static int first_time = 1;
if(!auto_package)
return 0;
assert(ord >= 0);
assert(ord < unit_count);
unit = table[ord];
assert(unit != NULL);
if(first_time)
{
mkdir(bindings_dir(), 0777);
first_time = 0;
}
cur_unit = ord;
cur_spec_fd = fopen(unit->unit_path, "w");
if(cur_spec_fd == NULL)
{
syserr(unit->unit_path, 0);
return 1;
}
cur_body_fd = fopen(unit->unit_body_path, "w");
if(cur_body_fd == NULL)
{
syserr(unit->unit_body_path, 0);
return 1;
}
output_to_spec(); /* start out with spec in new unit */
inform(0, 0, "Generating %s %s", unit->unit_path, unit->unit_body_path);
return 0;
}
void unit_completed()
{
if(auto_package)
{
fclose(cur_spec_fd);
fclose(cur_body_fd);
cur_unit = MAX_UNIQ_FNAMES + 1;
}
}
void unit_dependency(unit_n ord, unit_n dep, bool from_body)
/* unit <ord> depends on unit <dep> */
{
unit_t* ord_unit;
unit_n last;
if(!auto_package)
return;
last = unit_count;
assert(ord >= 0 && ord < last);
assert(dep >= 0 && dep < last);
ord_unit = table[ord];
assert(ord_unit != NULL);
set_reference(from_body ? ord_unit->from_body : ord_unit->unit_ref, dep);
}
char* unit_name(unit_n ord)
{
unit_t* unit;
if(!auto_package)
return "y";
assert(ord < unit_count);
unit = table[ord];
assert(unit != NULL);
assert(unit->unit_name != NULL);
return unit->unit_name;
}
char* cur_unit_name()
{
return unit_name(current_unit());
}
char* cur_unit_source()
{
unit_t* unit;
unit_n ord;
if(!auto_package)
return NULL;
ord = current_unit();
unit = table[ord];
assert(unit != NULL);
assert(unit->src_path != NULL);
return unit->src_path;
}
char* cur_unit_path()
{
unit_t* unit;
int ord;
if(!auto_package)
return NULL;
ord = current_unit();
unit = table[ord];
assert(unit != NULL);
assert(unit->unit_path != NULL);
return unit->unit_path;
}
static bool program_has_const_string = false;
void set_cur_unit_has_const_string(void)
{
unit_t* unit;
unit_n ord;
if(auto_package)
{
ord = cur_unit;
if(ord < unit_count)
{
unit = table[ord];
assert(unit != NULL);
unit->unit_has_const_string = true;
}
}
else
{
program_has_const_string = true;
}
}
int cur_unit_has_const_string(void)
{
unit_t* unit;
int ord;
if(auto_package)
{
ord = current_unit();
unit = table[ord];
assert(unit != NULL);
return unit->unit_has_const_string;
}
else
{
return program_has_const_string;
}
}
static int is_child_of_predef;
void set_cur_unit_is_child_of_predef()
{
char* unit = cur_unit_name();
int i = strlen(predef_pkg);
/*
* If we are not a child of the predefined package, "with" it,
* so we get the built-in types
*/
is_child_of_predef = ((!strncmp(predef_pkg, unit, i)) && (unit[i] == '.'));
}
int cur_unit_is_child_of_predef()
{
return is_child_of_predef;
}
static unit_n nth_element(unit_ref_t set1, unit_ref_t set2, int n)
/* returns the <n>th element of (<set1>-<set2>) */
/* set2 may be NULL */
{
int count;
unit_n i;
unit_n last = unit_count;
for(count = 0, i = 0; i < last; i++)
{
if(is_referenced(set1, i) && !(set2 && is_referenced(set2, i)))
{
if(count == n)
{
return i;
}
count++;
}
}
return -1;
}
int nth_ref_unit_ord(int n)
/* Returns the <n>th dependency of the current unit, or -1. */
{
unit_t* unit;
if(!auto_package)
return -1;
unit = table[current_unit()];
assert(unit != NULL);
clear_reference(unit->unit_ref, current_unit());
return nth_element(unit->unit_ref, 0, n);
}
int nth_direct_ref_unit_ord(int n)
/* Returns the <n>th "direct reference" of the current unit, or -1 */
{
unit_t* unit;
if(!auto_package)
return -1;
unit = table[current_unit()];
assert(unit != NULL);
return nth_element(unit->direct_ref, 0, n);
}
int nth_body_ref_unit_ord(int n)
{
unit_t* unit;
if(!auto_package)
return -1;
unit = table[current_unit()];
assert(unit != NULL);
clear_reference(unit->from_body, current_unit());
return nth_element(unit->from_body, unit->unit_ref, n);
}
/* Switch output to the current spec or body */
void output_to_spec(void)
{
cur_unit_fd = cur_spec_fd;
format_to_spec();
}
void output_to_body()
{
cur_unit_fd = cur_body_fd;
format_to_body();
}
void output_to(bool to_spec)
{
if(to_spec)
{
output_to_spec();
}
else
{
output_to_body();
}
}
bool output_is_spec()
{
return (cur_unit_fd == cur_spec_fd);
};
/* Set and get unit information fields */
comment_block_pt cur_unit_header_comment(void)
{
return table[cur_unit]->header_comment;
}
bool cur_unit_header_comment_set(void)
{
return table[cur_unit]->header_comment_set;
}
void set_cur_unit_header_comment(comment_block_pt comment)
{
unit_t* cu = table[cur_unit];
cu->header_comment = comment;
cu->header_comment_set = true;
}
comment_block_pt cur_unit_trailer_comment(void)
{
return table[cur_unit]->trailer_comment;
}
void set_cur_unit_trailer_comment(comment_block_pt comment)
{
table[cur_unit]->trailer_comment = comment;
}
file_id_t file_partner(file_id_t file)
{
char* file_id = file_name_from_ord(file);
char* partner_name = configured_source_partner(file_id);
if(partner_name)
{
file_pos_t pos = add_file(partner_name);
return pos_file(pos);
}
else
{
return -1;
}
}
int num_units()
{
return unit_count;
}
unit_n file_unit(file_id_t file)
{
unit_n result;
file_id_t partner;
if(file_unit_map[0] == -2)
{
/* This is a special signal that <file_unit_map>
* hasn't been initialized */
int i;
for(i = 0; i < MAX_UNIQ_FNAMES; i++)
file_unit_map[i] = -1;
}
result = file_unit_map[file];
if(result >= 0)
return result;
result = unit_count++;
file_unit_map[file] = result;
partner = file_partner(file);
if(partner >= 0)
{
file_unit_map[partner] = result;
}
return result;
}
unit_n pos_unit(file_pos_t pos)
{
unit_n result;
if(auto_package)
{
result = file_unit(pos_file(pos));
}
else
{
result = 0;
}
return result;
}
bool pos_in_current_unit(file_pos_t pos)
{
return pos_unit(pos) == current_unit();
}