-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodemodel.ml
More file actions
1264 lines (1175 loc) · 56.9 KB
/
codemodel.ml
File metadata and controls
1264 lines (1175 loc) · 56.9 KB
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
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
open Str;;
open Util;;
(* control how to see code output: *)
module String_set = Set.Make(String);;
let seen = ref String_set.empty;;
let args = Array.to_list(Sys.argv);;
let fullExpansion = List.exists (function x->x="-f") args;;
let guardedPreamble = List.exists (function x->x="--guarded-preamble") args;;
let noPreamble = List.exists (function x->x="--no-preamble") args;;
(* control how we increment and decrement refcounts. can be overridden. *)
let useAtomicOps = List.exists (function x->x="-atomic_ops") args;;
let add_in_type_to x = List.map (function name -> "in_" ^ name ^ "_type") x ;;
let add_type_to x = List.map (function name -> name ^ "_type") x ;;
let add_class_to x = List.map (function name -> "class " ^ name) x ;;
let comma_sep x = String.concat ", " x ;;
let template_def x = "template < " ^ (comma_sep (add_class_to x)) ^ " > ";;
let template_def_if_necessary x = (if List.length x > 0 then template_def x else "");;
let template_args x = " < " ^ (comma_sep (x)) ^ " > ";;
let template_args_if_necessary x = (if List.length x > 0 then template_args x else "");;
(* helper functions *)
let first (x,y) = x;;
let second (x,y) = y;;
let list_pair_cons ((a1,b1),(a2,b2)) = (a1::a2, b1::b2);;
let sep separ strfun lst = List.fold_left( function so_far -> function v -> so_far ^ (if (so_far = "") then "" else separ) ^ strfun(v) ) ("") (lst);;
let sep_with_indices separ strfun lst = first(List.fold_left( function (so_far,ix) -> function v -> (so_far ^ (if (so_far = "") then "" else separ) ^ strfun(v,ix), ix + 1) ) ("",0) (lst));;
let map_with_indices f lst = List.rev(first(List.fold_right( function v -> function (so_far,ix) -> (f(v,ix) :: so_far, ix + 1) ) (List.rev lst) ([],0) ));;
let newline = if fullExpansion = true then "\n" else "";;
let process_whitespace = if fullExpansion = true then (function w -> w) else (function w->w);;
(*tabify a string *)
let rec tabify s =
String.concat "" ("\t" :: (List.map (function t -> if t = '\n' then "\n\t" else (String.make 1 t)) (str_to_list s) ) )
and
str_to_list s =
str_to_list_num s 0
and
str_to_list_num s n = if n < String.length(s) then (s.[n] :: str_to_list_num s (n + 1)) else []
;;
let cppmlMatchVariableIncrementer = ref 0;;
let cppmlMatchVariableName baseName = (
let curRefCt = !cppmlMatchVariableIncrementer in(
cppmlMatchVariableIncrementer := curRefCt + 1;
baseName ^ "_" ^ string_of_int(curRefCt)
));;
(* type definitions *)
type named_type_op = NoTypeOp | TypeOp of string;;
(* representation of what's coming out of the parser
this is a very simple parse of the C++ - it mostly contains simple
grouping and scoping information so that we can correctly generate
@type information within namespaces and classes
*)
type
whitespace = string
and code =
Token of string
| Whitespace of string
| Sequence of code list
| Grouping of scoping * code
| Type of (template_term list) * ml_type list
| MatchExpression of string * code * match_term list
and scoping =
ClassOrNamespaceScope of string
| TemplateScope of template_term list
and template_term = string * string * template_default_value
and template_default_value = NoTemplateDefault | TemplateDefault of string
and optional_member_name =
Named of string
| Unnamed
and unnamed_type =
CPPTypes of (string * optional_member_name * whitespace) list * (string * optional_member_name * whitespace * code) list (* second is memo-ed definitions *)
and
optional_memo =
NoMemo
| Memo of whitespace * code
and named_type =
NamedType of string * whitespace * unnamed_type * named_type_op * whitespace
and ml_type_body =
Alternatives of named_type list * ml_common_type
| SimpleType of unnamed_type
and ml_common_type =
NoCommonBody
| TupleCommonBody of unnamed_type
and ml_type_name = string
and ml_type = ml_type_name * whitespace * ml_type_body * arbitrary_code_body
and arbitrary_code_body = code
and match_predicate = code
and match_term = match_pattern * whitespace * match_predicate
and match_pattern =
VariableMatch of string * whitespace
| TagMatch of string * whitespace * (match_pattern list) * whitespace * match_pattern
| TupleMatch of whitespace * match_pattern list
| ThrowawayMatch of whitespace
;;
let rec extractWhitespace code =
match code with
Token(_) -> (code, "")
| Whitespace(w) -> (Whitespace(""), w)
| Sequence([]) -> (code,"")
| Sequence(h::t) -> (
match (extractWhitespace(h),extractWhitespace(Sequence(t))) with
((hC,hW),(Sequence(tC),tW)) -> (Sequence(hC :: tC), hW ^ " " ^ tW)
)
| Type(_) -> (code,"")
(* TODO BUG brax: whitespace could be hidden in here... *)
| MatchExpression(_) -> (code,"")
;;
let rec
makeCPPTypes opts =
let regular, memoed = extractCPPs opts in
CPPTypes(regular, memoed)
and
extractCPPs opts =
match opts with
[] -> [],[]
| (a,b,c,NoMemo) :: tail -> (match extractCPPs tail with t1, t2 -> ((a,b,c) :: t1, t2))
| (a,b,c,Memo(w,code)) :: tail -> (match extractCPPs tail with t1, t2 -> (t1, ((a,b,c ^ w, code) :: t2)))
;;
(* represents the current "scoping environment" - gives us enough information to fully qualify a type *)
type
scope_envirionment =
Root
| ClassOrNamespace of string * scope_envirionment
| Template of string * (template_term list) * scope_envirionment
| UnboundTemplate of (template_term list) * scope_envirionment
;;
let rec
fully_qualified_scope_prefix environment =
match environment with
Root -> "::"
| ClassOrNamespace(name, prior) -> (fully_qualified_scope_prefix prior) ^ name ^ "::"
| Template(name, terms, prior) -> (fully_qualified_scope_prefix prior) ^ name ^ " < " ^ (sep "," (function (qual, term,def) -> term) terms) ^ " > " ^ "::"
and
fully_qualified_scope_name environment =
match environment with
Root -> ""
| ClassOrNamespace(name, prior) -> (fully_qualified_scope_prefix prior) ^ name
| Template(name, terms, prior) -> (fully_qualified_scope_prefix prior) ^ name ^ " < " ^ (sep "," (function (qual, term,def) -> term) terms) ^ " > "
and
fully_qualified_name environment name =
(fully_qualified_scope_prefix environment) ^ name
;;
let add_class_scope scope classname =
match scope with
UnboundTemplate(terms, prior) -> Template(classname, terms, prior)
| _ -> ClassOrNamespace(classname, scope)
;;
let add_template_scope scope terms = UnboundTemplate(terms, scope)
;;
let rec
scope_is_template scope =
match scope with
Root -> false
| ClassOrNamespace(n,prior) -> scope_is_template(prior)
| Template(s,l,e) -> true
| UnboundTemplate(s,e) -> true
;;
(*
A very simple representation of the final C++.
We make the following additional rules:
(1) within TemplatedScope, all cpp types get "typename" put in front of them
(2) functions get defined inline inside of templates, but are placed at the end of the file otherwise.
- member functions get the fully qualified typename placed in front
*)
type
cpp_output = cpp_output_element list
and cpp_output_element =
Text of string
| SeveralCPPEleements of cpp_output
| SourceWhitespace of string
| Block of cpp_output
| Private of cpp_output
| Public of cpp_output
| CPPType of string
| TemplatedScope of string * cpp_output
| Class of string * cpp_output
| FunctionDef of function_signature * function_preamble * function_body
| PostAmble of cpp_output
and function_signature =
FreeFunction of string * string
| MemberFunction of bool * bool * cpp_output_element * string * string (* isStatic, isTemplate, return type, fullyqualified typename, name/sig *)
and function_body = cpp_output
and function_preamble = cpp_output
;;
(* code to render cpp_output to native C++ *)
(* makes "template<class T ...>" *)
let template_terms_to_template_def terms =
match terms with
[] -> ""
| _ -> "template < " ^ (sep "," (function (qual, term,def) -> qual ^ " " ^ term) terms) ^ " > ";;
(* makes "<T, ...>" *)
let template_terms_to_template_spec terms =
match terms with
[] -> ""
| _ -> " < " ^ (sep "," (function (qual, term,def) -> term) terms) ^ " > ";;
(* creates a Class or TemplatedScope object depending on the current scope *)
let class_creator template_terms (name, body) =
match template_terms with
[] -> Class(name, body)
| _ -> TemplatedScope((template_terms_to_template_def template_terms), Class(name, body) :: [])
;;
let class_creator_fwd template_terms name =
match template_terms with
[] -> Text("class " ^ name ^ ";" ^ newline)
| _ -> Text((template_terms_to_template_def template_terms) ^ " class " ^ name ^ ";" ^ newline)
;;
let stripRootScopeResolution qualname =
if String.length qualname > 2 then
if String.sub qualname 0 2 = "::" then
String.sub qualname 2 ((String.length qualname) - 2)
else
qualname
else
qualname
;;
let lastSubstringPlusOne to_find to_search = (
try (Str.search_backward (Str.regexp_string to_find) to_search (String.length to_search)) + 1
with | e -> 0
);;
let typenameKeywordIsNecessary name = (
let lastDoubleColons = (lastSubstringPlusOne "::" (" " ^ name)) in
let lastCloseSharp = (lastSubstringPlusOne ">" (" " ^ name)) in
lastDoubleColons > 0 && lastDoubleColons > lastCloseSharp
)
;;
(* function to make a fully qualified name for "name" in the root environment *)
let qualified_namer scope template_terms name =
(fully_qualified_name scope name) ^ (template_terms_to_template_spec template_terms)
;;
let rec cpp_output_to_string cppo =
cpp_output_to_string_first false cppo
^ cpp_output_to_string_second false cppo
^ cpp_output_to_string_postamble cppo
and
cpp_output_to_string_first inside_template cppo =
String.concat "" (List.map (cpp_output_element_to_string_first inside_template) cppo)
and
cpp_output_element_to_string_first inside_template cppo =
match cppo with
Text(s) -> s
| SeveralCPPEleements(o) -> cpp_output_to_string_first inside_template o
| SourceWhitespace(s) -> process_whitespace(s)
| Block(o) -> "\t{" ^ newline ^ tabify(cpp_output_to_string_first inside_template o) ^ newline ^ "\t}" ^ newline
| Class(name, body) -> "class " ^ name ^ " {" ^ newline ^ cpp_output_to_string_first inside_template body ^ newline ^ "};" ^ newline
| TemplatedScope(name, body) -> name ^ cpp_output_to_string_first true body
| CPPType(string) -> (if inside_template = true && (typenameKeywordIsNecessary string) then "typename " else "") ^ string
| Private(body) -> "private: " ^ newline ^ tabify(tabify(cpp_output_to_string_first inside_template body)) ^ newline
| Public(body) -> "public: " ^ newline ^ tabify(tabify(cpp_output_to_string_first inside_template body)) ^ newline
| FunctionDef(FreeFunction(rettype, s), prebody, body) ->
" inline friend " ^
rettype ^ " " ^ s ^
(let res = tabify(tabify((cpp_output_to_string_first inside_template prebody))) in if res = "" then "" else res ^ newline)
^ "\t{" ^ newline ^ tabify( cpp_output_to_string_first inside_template body ) ^ newline ^ "\t}" ^ newline
| FunctionDef(MemberFunction(isStatic, isTemplate, rettype, qualName, s), prebody, body) ->
if inside_template then
(* we can go ahead and fully define the function *)
(cpp_output_to_string_first inside_template (rettype :: [])) ^ " " ^ s ^
(let res = tabify(tabify((cpp_output_to_string_first inside_template prebody))) in if res = "" then "" else res ^ newline)
^ "\t{" ^ newline ^ tabify( cpp_output_to_string_first inside_template body ) ^ newline ^ "\t}" ^ newline
else
(* we're just forward declaring it *)
(cpp_output_to_string_first inside_template (rettype :: [])) ^ " " ^ s ^ ";" ^ newline
| PostAmble(x) -> ""
and
cpp_output_to_string_second inside_template cppo =
String.concat "" (List.map (cpp_output_element_to_string_second inside_template) cppo)
and
cpp_output_element_to_string_second inside_template cppo =
if inside_template = true then "" else
match cppo with
Text(s) -> ""
| SeveralCPPEleements(s) -> cpp_output_to_string_second inside_template s
| SourceWhitespace(s) -> ""
| Private(s) -> cpp_output_to_string_second inside_template s
| Public(s) -> cpp_output_to_string_second inside_template s
| CPPType(s) -> ""
| Block(o) -> cpp_output_to_string_second inside_template o
| TemplatedScope(name, body) -> cpp_output_to_string_second true body
| Class(name, body) -> cpp_output_to_string_second inside_template body
| FunctionDef(FreeFunction(rettype, s), prebody, body) -> ""
| FunctionDef(MemberFunction(isStatic, isTemplate, rettype, qualifiedName, s), prebody, body) ->
(if isTemplate then "" else " inline ") ^
(cpp_output_to_string_first inside_template (rettype::[])) ^ " " ^ stripRootScopeResolution(qualifiedName) ^ " :: " ^ s ^ newline ^ (
let res = tabify(tabify((cpp_output_to_string_first inside_template prebody))) in if res = "" then "" else res ^ newline)
^ "\t{" ^ newline ^ tabify( cpp_output_to_string_first inside_template body ) ^ newline ^ "\t}" ^ newline
| PostAmble(x) -> ""
and
cpp_output_to_string_postamble cppo =
String.concat "" (List.map (cpp_output_to_string_element_postamble) cppo)
and
cpp_output_to_string_element_postamble cppo =
match cppo with
Text(s) -> ""
| SeveralCPPEleements(s) -> cpp_output_to_string_postamble s
| SourceWhitespace(s) -> ""
| Private(s) -> cpp_output_to_string_postamble s
| Public(s) -> cpp_output_to_string_postamble s
| CPPType(s) -> ""
| Block(o) -> cpp_output_to_string_postamble o
| TemplatedScope(name, body) -> cpp_output_to_string_postamble body
| Class(name, body) -> cpp_output_to_string_postamble body
| FunctionDef(_, prebody, body) -> ((cpp_output_to_string_postamble prebody) ^ (cpp_output_to_string_postamble body))
| PostAmble(x) -> cpp_output_to_string x
;;
(* functions to render resulting parse tree back into cpp_output *)
let rec
(* map the parse tree to cpp_output, keeping track of the current scoping *)
code_to_cpp_output scope c =
match c with
Token(t) -> Text(t) :: []
| Whitespace(w) -> SourceWhitespace(w) :: []
| Sequence([]) -> []
| Sequence(t :: t2) -> (code_to_cpp_output scope t) @ (code_to_cpp_output scope (Sequence(t2)))
| Grouping(TemplateScope(terms), code) -> TemplatedScope("", code_to_cpp_output (add_template_scope scope terms) (code)) :: []
| Grouping(ClassOrNamespaceScope(classname), body) -> code_to_cpp_output (add_class_scope scope classname) body
| Type(terms, mlt) -> (ml_type_list_to_cpp scope terms mlt)
| MatchExpression(t, expr, matchers ) -> matchers_to_cpp(t ^ "::self_type",expr,matchers)
and
(* render a particular ml_type group. operates in three parts: forward declarations, the main body, and then
supplemental types (e.g. the common_data, alternative bodies, etc.) *)
ml_type_list_to_cpp scope template_terms mlt =
List.flatten(List.map (ml_type_class_def_forward scope template_terms) mlt)
@ List.flatten(List.map (ml_type_class_def_body scope template_terms) mlt)
@ List.flatten(List.map (ml_type_class_def_parts scope template_terms) mlt)
and
(* renders a single type forward *)
ml_type_class_def_forward scope template_terms (name, wh0, mlt, arb_body_code) =
(
match mlt with
SimpleType(unnamed) -> (ml_tuple_class_forward scope template_terms name)
| Alternatives(named_types, common) -> (ml_alternative_class_forward scope template_terms name named_types arb_body_code)
)
and
(* renders a single type body *)
ml_type_class_def_body scope template_terms (name, wh0, mlt, arb_body_code) =
SourceWhitespace(wh0) :: [] @
(
match mlt with
SimpleType(unnamed) -> (ml_tuple_class_body scope template_terms (name, "", unnamed, arb_body_code))
| Alternatives(named_types, common) ->
List.flatten(List.map (ml_named_type_body_extract_whitespace scope template_terms name) named_types) @
(ml_alternative_class_body scope template_terms name named_types common arb_body_code)
)
and
(* renders a single type's subparts *)
ml_type_class_def_parts scope template_terms (name, wh0, mlt, arb_body_code) =
(
match mlt with
SimpleType(unnamed) -> []
| Alternatives(named_types, common) ->
List.flatten(List.map (ml_named_type_body scope template_terms name) named_types)
@ ml_type_common_data_class scope template_terms name named_types common :: []
)
and
(* Forward decls for Alternatives *)
ml_alternative_class_forward scope template_terms name named_types arb_body_code =
(* define a function to create classes in this environment *)
let classmaker = class_creator_fwd template_terms in
(classmaker(name ^ "_tags") ::
classmaker(name ^ "_common_data") ::
classmaker(name) ::
[]) @
(List.map (function nt -> match nt with
NamedType(subname, wh1, unnamed, o, wh2) -> classmaker(name ^ "_" ^ subname ^ "Type")) named_types
)
and
(* Forward decls for Alternatives *)
ml_tuple_class_forward scope template_terms name =
(* define a function to create classes in this environment *)
let classmaker = class_creator_fwd template_terms in
classmaker(name) ::
[]
and
generateEnumClassType classname enumtext = (
"class " ^ classname ^ "{ public: " ^
"typedef "^classname ^" self_type;" ^
classname ^ "(int i) : m(i) {} " ^
"bool operator==(const int& in) const { return m == in; } " ^
"operator int () const { return m; } " ^
"enum { "^ enumtext ^ "}; " ^
"private: int m;" ^
"};"
)
and
(* Bodies for Alternatives *)
ml_alternative_class_body scope template_terms name named_types common arb_body_code =
(* define a function to create classes in this environment *)
let classmaker = class_creator template_terms in
let fullyQualifiedNamer = qualified_namer scope template_terms in
let ownFullName = fullyQualifiedNamer(name) in
let commonTypes = (match common with TupleCommonBody(CPPTypes(c,mems)) -> c | NoCommonBody -> []) in
let commonMemos = (match common with TupleCommonBody(CPPTypes(c, memos)) -> memos | NoCommonBody -> []) in
let commaGlue a b = a ^ (if String.length(a) > 0 && String.length(b) > 0 then "," else "") ^ b in
let commonConstructorArgs = if List.length(commonTypes) > 0 then (sep_with_indices "," (function(c, ix)-> "const member_" ^ string_of_int(ix) ^ "_type& common_" ^ string_of_int(ix)) commonTypes) else "" in
let commonConstructorInitializers = if List.length(commonTypes) > 0 then (sep_with_indices "," (function(c, ix)-> "common_" ^ string_of_int(ix) ) commonTypes) else "" in
let commonConstructorDefaultInits = if List.length(commonTypes) > 0 then (sep_with_indices "," (function(c, ix)-> "member_" ^ string_of_int(ix) ^ "_type()" ) commonTypes) else "" in
let first_tag_name = (match named_types with NamedType(tag,_,_,_,_)::tail -> tag | _ -> "err") in
let data_expr_from_tag constness_string tag =
Text("mReference.getData( (" ^ tag ^ "Type*)0) ")
:: []
in
let member_type_for ix = ownFullName ^ "::member_" ^ string_of_int(ix) ^ "_type" in
let member_type_for_as_CPPType ix = CPPType(ownFullName ^ "::member_" ^ string_of_int(ix) ^ "_type") in
let common_data_type_as_CPPType = CPPType(ownFullName ^ "::common_data_type &") in
let const_common_data_type_as_CPPType = CPPType(ownFullName ^ "::common_data_type const& ") in
classmaker(name,
Public(
Text("typedef ") :: CPPType(fullyQualifiedNamer(name)) :: Text(" self_type;" ^ newline)
:: Text(generateEnumClassType "tag_type" (sep "," (function nt -> match nt with NamedType(name, wh1, unnamed, o, wh2) -> name) named_types))
:: Text("typedef ::CPPML::TaggedUnionReference<self_type, void> tagged_union_reference_type;" ^ newline)
:: Text("typedef ") :: CPPType(fullyQualifiedNamer(name ^ "_common_data")) :: Text(" common_data_type;" ^ newline)
:: (map_with_indices (function((c,nm,w), ix)->
SeveralCPPEleements(Text("typedef ") :: CPPType(c ^ " member_" ^ string_of_int(ix) ^ "_type") :: Text(";" ^ w ^ newline) :: [])
) commonTypes) @
(map_with_indices (function((c,nm,w,def), ix)->
SeveralCPPEleements(Text("typedef ") :: CPPType(c ^ " memo_member_" ^ string_of_int(ix) ^ "_type") :: Text(";" ^ w ^ newline) :: [])
) commonMemos) @
FunctionDef(MemberFunction(false, false, Text("const char*"), ownFullName, "tagName(void) const"), [], Text(
(sep "" (
function nt -> match nt with NamedType(tagname, wh1, unnamed, o, wh2) -> "if (this->mReference.getTag() == tag_type::" ^ tagname ^ ") return \"" ^ tagname ^ "\";"
)
named_types)
^ " return \"\";"
) ::
[]
)
:: FunctionDef(MemberFunction(false, false, CPPType(" ::CPPML::Refcount< " ^ ownFullName ^ ", void>::refcount_type"), ownFullName, "refcount(void) const"), [], Text("return this->mReference.getRefcount();") :: [])
:: List.flatten(List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
Text("typedef ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text(" " ^ tag ^ "Type;" ^ newline) :: []
) named_types)
)
::Public(
(map_with_indices (
function((c,nm,w), ix)->
SeveralCPPEleements(
Text("class getter_common_" ^ string_of_int(ix) ^ " { public: "
^ " static const ")
:: member_type_for_as_CPPType(ix)
:: Text("& get(const self_type& s) { /* ASDF */ return s." ^ member_name_for(ix,nm) ^ "(); } " ^ " static ")
:: member_type_for_as_CPPType(ix)
:: Text("& get(self_type& s) { return s." ^ member_name_for(ix,nm) ^ "(); } " ^
" static const char* name(void) { return \"" ^ member_name_for(ix,nm) ^ "\"; } " ^ " };" ^ newline)
:: [])
) commonTypes)
)
::Public(
(map_with_indices (function((c,nm,w,def), ix)-> FunctionDef(
MemberFunction(false,false,
SeveralCPPEleements(
Text("const ") ::
CPPType(ownFullName ^ "::memo_member_" ^ string_of_int(ix) ^ "_type&") ::
[]
),
ownFullName,
(match nm with
Unnamed -> "getMemo" ^ string_of_int(ix)
| Named(n) -> n)
^ ("(void) const")
),
[],
(Text("return mReference.getCommonData().memodata_m_" ^ string_of_int(ix) ^ ".get([&](){ return ") :: [])
@ (code_to_cpp_output scope def) @ (Text("; });") :: [])
)
)
commonMemos)
)
::Public(map_with_indices (function(t, ix)->
match t with NamedType(tag, wh1, unnamed_type, o, wh2) ->
match unnamed_type with CPPTypes(cpp_types, memos) ->
SeveralCPPEleements(
Text("class getter_" ^ tag ^ " { public: "
^ " static const ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& get(const self_type& s, bool check = true) { return s.get" ^ tag ^ "(check); } " ^ newline
^ " static const ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& getConst(const self_type& s, bool check = true) { return s.get" ^ tag ^ "(check); } " ^ newline
^ " static ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& getNonconst(self_type& s, bool check = true) { return s.get" ^ tag ^ "(check); } " ^ newline
^ " static ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& get(self_type& s, bool check = true) { return s.get" ^ tag ^ "(check); } " ^ newline
^ " static " ^ name ^ " constructor(" ^ (sep_with_indices "," (function((c,nm,w),ix)->"const " ^ c ^ "& in" ^ string_of_int(ix)) cpp_types) ^ ") {"
^ "return self_type:: " ^ tag ^ "(" ^ (commaGlue commonConstructorDefaultInits (sep_with_indices "," (function((c,nm,w),ix)->"in" ^ string_of_int(ix)) cpp_types)) ^ "); }" ^ newline
^ " static bool is(const self_type& s) { return s.is" ^ tag ^ "(); } " ^ newline
^ " static const char* name(void) { return \"" ^ tag ^ "\"; } " ^ newline
^ " };" ^ newline)
:: []
)
) named_types)
::Public(
Text("typedef ::CPPML::Kinds::alternative kind;" ^ newline )
:: Text("typedef ") ::
metadata_to_chain(
(
map_with_indices (function(t, ix)->
match t with NamedType(tag, wh1, unnamed_type, o, wh2) ->
SeveralCPPEleements(Text(" ::CPPML::Alternative< self_type , ") ::
CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text(" , "
^ " getter_" ^ tag
^ " > ") :: []
)
) named_types
)
@
(
map_with_indices (
function(t, ix)->
Text(" ::CPPML::AlternativeCommonMember< self_type, "
^ "self_type::member_" ^ string_of_int(ix) ^ "_type, "
^ "getter_common_" ^ string_of_int(ix) ^ ", "
^ string_of_int(ix)
^ " > "
)
)
commonTypes
)
)
:: Text(" metadata;")
:: []
)
::Private(
Text("tagged_union_reference_type mReference;" ^ newline)
:: FunctionDef(MemberFunction(false, false, Text("void"), ownFullName, "drop(void)"), [],
Text("if (mReference.decrementRefcount()) {"^newline^"\t") ::
Text("switch (mReference.getTag()) ") ::
Block(
List.map (
function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
SeveralCPPEleements(
Text("case tag_type::" ^ tag ^ " : mReference.destroyAs(( ")
:: CPPType(
fullyQualifiedNamer(name ^ "_" ^tag^"Type"))
:: Text(" *)0); break; " ^ newline)
:: []
)
) named_types
) ::
Text(newline ^ "\t}") ::
[]
)
:: []
)
::Public(
FunctionDef(MemberFunction(true, true, Text("template<class funtype__> void"), ownFullName, " callback(const funtype__& inFunc, tag_type inTag) const"), [],
Text("switch (inTag) ") ::
Block(
List.map (
function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
SeveralCPPEleements(
Text("case tag_type::" ^ tag ^ " : inFunc(( ")
:: CPPType(
fullyQualifiedNamer(name ^ "_" ^tag^"Type"))
:: Text(" *)0); break; " ^ newline)
:: []
)
) named_types
) ::
[]
)
:: []
)
::Public(
FunctionDef(MemberFunction(false, true, Text("template<class subtype__> const subtype__&"), ownFullName, " get(subtype__* deliberatelyZero) const"), [],
Text("return mReference.getData(deliberatelyZero);") :: []
)
:: []
)
::Public(
FunctionDef(MemberFunction(false, true, Text("template<class subtype__> subtype__&"), ownFullName, " get(subtype__* deliberatelyZero)"), [],
Text("return mReference.getData(deliberatelyZero);") :: []
)
:: []
)
::Public(
FunctionDef(MemberFunction(false, true, Text("template<class funtype__> void "), ownFullName, " visit(const funtype__& inFunc) const"), [],
Text("switch (mReference.getTag()) ") ::
Block(
List.map (
function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
SeveralCPPEleements(
Text("case tag_type::" ^ tag ^ " : inFunc(mReference.getData(( ")
:: CPPType(
fullyQualifiedNamer(name ^ "_" ^tag^"Type"))
:: Text(" *)0)); break; " ^ newline)
:: []
)
) named_types
) ::
[]
)
:: []
)
::Private(
FunctionDef(MemberFunction(false, false, Text("/*pointer constructor*/"), ownFullName, name ^ "(const tagged_union_reference_type in)"), [],
Text("mReference = in;") :: [])
:: []
)
::Public(
FunctionDef(MemberFunction(false, false, Text(""), ownFullName,"~" ^ name ^ "()"), [], Text("drop();") :: [])
:: FunctionDef(
MemberFunction(
false,
false,
Text("/*empty constructor*/"),
ownFullName,
name ^ "()"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ first_tag_name ^ "," ^
"common_data_type(" ^ (commaGlue commonConstructorDefaultInits "") ^ "), " ^
first_tag_name ^ "Type()" ^
");") :: []
)
:: FunctionDef(MemberFunction(false, false, common_data_type_as_CPPType, ownFullName, "getCommonData(void) const"), [],
Text("return mReference.getCommonData();") :: [])
:: FunctionDef(MemberFunction(false, false, const_common_data_type_as_CPPType, ownFullName, "getCommonData(void)"), [],
Text("return mReference.getCommonData();") :: [])
:: FunctionDef(MemberFunction(false, false, Text("/*copy constructor*/"), ownFullName, name ^ "(const self_type& in)"), [],
Text("mReference = in.mReference; mReference.incrementRefcount();") :: [])
:: FunctionDef(MemberFunction(false, false, Text("/*copy constructor*/"), ownFullName, name ^ "(self_type&& in)"), [],
Text("mReference.swap(in.mReference);") :: [])
:: FunctionDef(MemberFunction(false, false, CPPType(ownFullName ^ "&"), ownFullName, "operator=(const self_type& in)"), [],
Text("in.mReference.incrementRefcount(); tagged_union_reference_type newRef =in.mReference; drop(); mReference = newRef; return *this;") :: [])
:: FunctionDef(MemberFunction(false, false, CPPType(ownFullName ^ "&"), ownFullName, "operator=(self_type&& in)"), [],
Text("mReference.swap(in.mReference); return *this;") :: [])
:: List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
FunctionDef(
MemberFunction(
false,
false,
Text("/*constructor no common*/"),
ownFullName,
name ^ "(" ^ (commaGlue commonConstructorArgs ("const " ^ tag ^ "Type& in")) ^ ")"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ tag ^ "," ^
"common_data_type(" ^ (commaGlue commonConstructorInitializers "") ^ "), " ^
"in" ^
");")
:: []
)
) named_types
@ List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
FunctionDef(
MemberFunction(
false,
false,
Text("/*constructor with common*/"),
ownFullName,
name ^ "(" ^ ("const " ^ tag ^ "Type& in, const common_data_type& inCommon") ^ ")"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ tag ^ "," ^
"inCommon, " ^
"in" ^
");")
:: []
)
) named_types
@ (if String.length(commonConstructorArgs) > 0 then
List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
FunctionDef(
MemberFunction(
false,
false,
Text("/*constructor with move semantics no common*/"),
ownFullName,
name ^ "(" ^ tag ^ "Type&& in)"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ tag ^ "," ^
"common_data_type(" ^ (commaGlue commonConstructorDefaultInits "") ^ "), " ^
"::CPPML::forward<" ^ tag ^"Type>(in)" ^
");")
:: []
)
) named_types
else []
)
@ (if String.length(commonConstructorArgs) > 0 then
List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
FunctionDef(
MemberFunction(
false,
false,
Text("/*constructor with move semantics with common*/"),
ownFullName,
name ^ "(" ^ tag ^ "Type&& in, common_data_type&& inCommon)"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ tag ^ "," ^
"::CPPML::forward<common_data_type>(inCommon), " ^
"::CPPML::forward<" ^ tag ^"Type>(in)" ^
");")
:: []
)
) named_types
else []
)
@ (if String.length(commonConstructorArgs) > 0 then
List.map (function n->
match n with NamedType(tag, wh1, unnamed_type, o, wh2) ->
FunctionDef(
MemberFunction(
false,
false,
Text("/*constructor 2*/"),
ownFullName,
name ^ "(const " ^ tag ^ "Type& in)"
),
[],
Text("mReference = tagged_union_reference_type::create(" ^
"tag_type::" ^ tag ^ "," ^
"common_data_type(" ^ (commaGlue commonConstructorDefaultInits "") ^ "), " ^
"in" ^
");")
:: []
)
) named_types
else []
)
@(map_with_indices (function((c,nm,w), ix)->
FunctionDef(
MemberFunction(
false,
false,
Text("const " ^ member_type_for(ix) ^ "& "),
ownFullName,
member_name_for(ix, nm) ^ "(void) const"
),
[],
Text("return mReference.getCommonData().m_" ^ string_of_int(ix) ^ ";" ^ newline) :: [])
)
commonTypes
)
@(map_with_indices (
function((c,nm,w), ix) ->
FunctionDef(
MemberFunction(false, false, Text(member_type_for(ix) ^ "& "), ownFullName, member_name_for(ix, nm) ^ "(void)"),
[],
Text("return mReference.getCommonData().m_" ^ string_of_int(ix) ^ ";" ^ newline) :: []
)
) commonTypes
)
@(map_with_indices (
function((c,nm,w), ix) ->
FunctionDef(
MemberFunction(false, false, Text("const " ^ member_type_for(ix) ^ "& "), ownFullName, "getM" ^ string_of_int(ix) ^ "(void) const"),
[],
Text("return mReference.getCommonData().m_" ^ string_of_int(ix) ^ ";" ^ newline) :: []
)
) commonTypes
)
)
::Public(
List.flatten(List.map (function t ->
match t with NamedType(tag, wh1, unnamed_type, o, wh2) ->
match unnamed_type with CPPTypes(cpp_types, memos) ->
Text("/*static constructor*/ static ")
:: FunctionDef(MemberFunction(false, false, CPPType(ownFullName), ownFullName, tag ^ "("^ (commaGlue commonConstructorArgs (sep_with_indices "," (function((c,nm,w),ix)->"const " ^ c ^ "& in" ^ string_of_int(ix)) cpp_types)) ^")"),
[], Text("return self_type(tagged_union_reference_type::create(tag_type::" ^ tag ^ ", common_data_type(" ^
(commaGlue commonConstructorInitializers "") ^ "), " ^
( (tag ^ "Type(" ^ (sep_with_indices "," (function((c,nm,w),ix)->"in" ^ string_of_int(ix)) cpp_types) ^ ")")) ^ "));") :: []
)
:: SeveralCPPEleements(
if String.length(commonConstructorArgs) > 0 then
Text("/*static constructor without common*/ static ")
:: FunctionDef(
MemberFunction(false, false, CPPType(ownFullName), ownFullName, tag ^ "("^
(sep_with_indices "," (function((c,nm,w),ix)->"const " ^ c ^ "& in" ^ string_of_int(ix)) cpp_types) ^")"),
[], Text("return self_type(tagged_union_reference_type::create(tag_type::" ^ tag ^ ", common_data_type(" ^ (commaGlue commonConstructorDefaultInits "") ^ "), " ^
( (tag ^ "Type(" ^ (sep_with_indices "," (function((c,nm,w),ix)->"in" ^ string_of_int(ix)) cpp_types) ^ ")")) ^ "));") :: []
) :: []
else
[]
)
:: FunctionDef(MemberFunction(false, false, SeveralCPPEleements(Text("const ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& ") :: []), ownFullName, "get" ^ tag ^ "(void) const"), [],
Text("return ") :: (data_expr_from_tag "const" tag) @ (Text(";")::[]))
:: FunctionDef(MemberFunction(false, false, SeveralCPPEleements(CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type"))::Text("& ") :: []), ownFullName, "get" ^ tag ^ "(void)"), [],
Text("return ") :: (data_expr_from_tag "" tag) @ (Text(";")::[]))
:: FunctionDef(MemberFunction(false, false, SeveralCPPEleements(Text("const ") :: CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& "):: []), ownFullName, "get" ^ tag ^ "(bool check) const"), [],
Text("if (check && !this->is" ^ tag ^ "()) CPPML::throwBadUnionAccess(*this); ")
:: Text("return ") :: (data_expr_from_tag "const" tag) @ (Text(";")::[]))
:: FunctionDef(MemberFunction(false, false, SeveralCPPEleements(CPPType(fullyQualifiedNamer(name ^ "_" ^ tag ^ "Type")) :: Text("& ") :: []), ownFullName, "get" ^ tag ^ "(bool check)"), [],
Text("if (check && !this->is" ^ tag ^ "()) CPPML::throwBadUnionAccess(*this);")
:: Text("return ") :: (data_expr_from_tag "" tag) @ (Text(";")::[]))
:: FunctionDef(MemberFunction(false, false, Text("bool"), ownFullName, "is" ^ tag ^ "(void) const"), [], Text("return mReference.getTag() == tag_type::" ^ tag ^ ";") :: [])
:: match o with
NoTypeOp -> Text("") :: []
| TypeOp(o) ->
FunctionDef(
FreeFunction(name, " operator" ^ o ^ "("^ (sep_with_indices "," (function((c,nm,w),ix)->"const " ^ c ^ "& in" ^ string_of_int(ix)) cpp_types) ^")"),
[],
Text("return ") :: Text("self_type") :: Text("::" ^ tag ^ "(" ^ (sep_with_indices "," (function((c,nm,w),ix)->"in" ^ string_of_int(ix)) cpp_types) ^ ");")
:: []
)
:: []
) named_types)
)
::Private(code_to_cpp_output scope arb_body_code)
:: []
)
:: []
and
ml_type_common_data_class scope template_terms name named_types common =
(* define a function to create classes in this environment *)
let classmaker = class_creator template_terms in
let fullyQualifiedNamer = qualified_namer scope template_terms in
let ownFullName = fullyQualifiedNamer (name ^ "_common_data") in
let commonTypes = (match common with TupleCommonBody(CPPTypes(c, memos)) -> c | NoCommonBody -> []) in
let commonMemos = (match common with TupleCommonBody(CPPTypes(c, memos)) -> memos | NoCommonBody -> []) in
let commonConstructorArgs =
if List.length(commonTypes) > 0 then
(sep_with_indices "," (function(c, ix)-> "const member_" ^ string_of_int(ix) ^ "_type& in" ^ string_of_int(ix)) commonTypes)
else ""
in
let commonConstructorInitializers =
if List.length(commonTypes) > 0 then
":" ^ (sep_with_indices "," (function(c, ix)-> "m_" ^ string_of_int(ix) ^ "(" ^ "in" ^ string_of_int(ix) ^ ")") commonTypes)
else ""
in
let commonConstructorInitializersFromCopyConstructorArg =
if List.length(commonTypes) > 0 then
":" ^ (sep_with_indices "," (function(c, ix)-> "m_" ^ string_of_int(ix) ^ "(" ^ "inToCopy.m_" ^ string_of_int(ix) ^ ")") commonTypes)
else ""
in
classmaker(name ^ "_common_data",
Public(
Text("typedef ") :: CPPType(fullyQualifiedNamer(name)) :: Text(" holding_type;" ^ newline)
:: (map_with_indices (function((c,nm,w), ix)-> Text("typedef " ^ c ^ " member_" ^ string_of_int(ix) ^ "_type;" ^ w ^ newline)) commonTypes) @
(map_with_indices (function((c,nm,w,def), ix)-> SeveralCPPEleements(Text("typedef ") :: CPPType(c) :: Text(" memo_member_" ^ string_of_int(ix) ^ "_type;" ^ w ^ newline) :: [])) commonMemos) @
(map_with_indices (function((c,nm,w,def), ix)->
SeveralCPPEleements(Text("typedef ") :: CPPType("::CPPML::MemoStorage<holding_type, memo_member_" ^ string_of_int(ix) ^ "_type, void>") ::
Text(" memo_member_storage_" ^ string_of_int(ix) ^ "_type;" ^ w ^ newline) :: [])) commonMemos
) @
FunctionDef(
MemberFunction(
false,
false,
Text(" /* common data constructor with individual arguments */ "),
ownFullName,
name ^ "_common_data(" ^ commonConstructorArgs ^ ")"
),
Text(commonConstructorInitializers) :: [],
(* initialize the memovalues *)
[]
)
::
FunctionDef(
MemberFunction(
false,
false,
Text(" /* common data copy constructor */ "),
ownFullName,
name ^ "_common_data(const " ^ name ^ "_common_data& inToCopy)"
),
Text(commonConstructorInitializersFromCopyConstructorArg) :: [],
(* initialize the memovalues *)
[]
)
::
FunctionDef(MemberFunction(false, false, Text(" /* common_data destructor */"),
ownFullName, "~" ^ name ^ "_common_data()"), [],
[]
)
::[]
)
(* allocate the common types *)
:: Public(
map_with_indices (
function((c,nm,w), ix)->
Text("member_" ^ string_of_int(ix) ^ "_type m_" ^ string_of_int(ix) ^ ";" ^ newline))
commonTypes
)
:: Public(
map_with_indices (
(* define the typedefs for the various memo objects *)
function((c,nm,w,def), ix)->
Text("mutable memo_member_storage_" ^ string_of_int(ix) ^ "_type memodata_m_" ^ string_of_int(ix) ^ ";" ^ newline)
)
commonMemos
)
::[]
)
and
member_name_for (ix, membername) =
match membername with
Unnamed -> "m" ^ string_of_int(ix)
| Named(s) -> s
and
ml_tuple_class_body scope template_terms (tag, suffix, unnamed_types, arb_body_code) =
(
let classmaker = class_creator template_terms in
let fullyQualifiedNamer = qualified_namer scope template_terms in
let ownFullName = fullyQualifiedNamer (tag ^ suffix) in
let member_type_for ix = ownFullName ^ "::member_" ^ string_of_int(ix) ^ "_type" in
match unnamed_types with CPPTypes(cpp_types, memos) ->
classmaker( tag ^ suffix,
Public(
Text("typedef " ^ tag ^ suffix ^ " self_type;" ^ newline)
:: (map_with_indices (function((c,nm,w), ix)-> Text("typedef " ^ c ^ " member_" ^ string_of_int(ix) ^ "_type;" ^ w ^ newline)) cpp_types)
@ (FunctionDef(MemberFunction(false, false, Text(""), ownFullName, "/*tuple constructor*/ " ^ tag ^ suffix ^ "(" ^ (sep_with_indices "," (function(c, ix)-> "const member_" ^ string_of_int(ix) ^ "_type& in" ^ string_of_int(ix)) cpp_types) ^ ") "),
Text(if List.length(cpp_types) > 0 then ":" ^ (sep_with_indices "," (function(c, ix)-> "m_" ^ string_of_int(ix) ^ "(" ^ "in" ^ string_of_int(ix) ^ ")") cpp_types) else "") ::
[],
Text("::CPPML::validate(*this);") :: []) :: [])
@ (