File: compute_lines.ml

package info (click to toggle)
coccinelle 1.0.8.deb-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 26,148 kB
  • sloc: ml: 136,392; ansic: 23,594; sh: 2,189; makefile: 2,157; perl: 1,576; lisp: 840; python: 823; awk: 70; csh: 12
file content (1397 lines) | stat: -rw-r--r-- 52,995 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
(*
 * This file is part of Coccinelle, licensed under the terms of the GPL v2.
 * See copyright.txt in the Coccinelle source code for more information.
 * The Coccinelle source code can be obtained at http://coccinelle.lip6.fr
 *)

(* Computes starting and ending logical lines for statements and
expressions.  every node gets an index as well. *)

module Ast0 = Ast0_cocci
module Ast = Ast_cocci

(* --------------------------------------------------------------------- *)
(* Result *)

(* This is a horrible hack.  We need to have a special treatment for the code
inside a nest, and this is to avoid threading that information around
everywhere *)
let in_nest_count = ref 0
let check_attachable v = if !in_nest_count > 0 then false else v

let mkres x e left right =
  let lstart = Ast0.get_info left in
  let lend = Ast0.get_info right in
  let pos_info =
    { Ast0.line_start = lstart.Ast0.pos_info.Ast0.line_start;
      Ast0.line_end = lend.Ast0.pos_info.Ast0.line_end;
      Ast0.logical_start = lstart.Ast0.pos_info.Ast0.logical_start;
      Ast0.logical_end = lend.Ast0.pos_info.Ast0.logical_end;
      Ast0.column = lstart.Ast0.pos_info.Ast0.column;
      Ast0.offset = lstart.Ast0.pos_info.Ast0.offset;} in
  let info =
    { Ast0.pos_info = pos_info;
      Ast0.whitespace = lstart.Ast0.whitespace;
      (* not clear that the next two lines serve any purpose *)
      Ast0.attachable_start = check_attachable lstart.Ast0.attachable_start;
      Ast0.attachable_end = check_attachable lend.Ast0.attachable_end;
      Ast0.mcode_start = lstart.Ast0.mcode_start;
      Ast0.mcode_end = lend.Ast0.mcode_end;
      (* only for tokens, not inherited upwards *)
      Ast0.strings_before = []; Ast0.strings_after = [];
      Ast0.isSymbolIdent = false; } in
  {x with Ast0.node = e; Ast0.info = info}

(* This looks like it is there to allow distribution of plus code
over disjunctions.  But this doesn't work with single_statement, as the
plus code has not been distributed to the place that it expects.  So the
only reasonably easy solution seems to be to disallow distribution. *)
(* inherit attachable is because single_statement doesn't work well when +
code is attached outside an or, but this has to be allowed after
isomorphisms have been introduced.  So only set it to true then, or when we
know that the code involved cannot contain a statement, ie it is a
declaration. *)
let inherit_attachable = ref false
let mkmultires x e left right (astart,start_mcodes) (aend,end_mcodes) =
  let lstart = Ast0.get_info left in
  let lend = Ast0.get_info right in
  let pos_info =
    { Ast0.line_start = lstart.Ast0.pos_info.Ast0.line_start;
      Ast0.line_end = lend.Ast0.pos_info.Ast0.line_end;
      Ast0.logical_start = lstart.Ast0.pos_info.Ast0.logical_start;
      Ast0.logical_end = lend.Ast0.pos_info.Ast0.logical_end;
      Ast0.column = lstart.Ast0.pos_info.Ast0.column;
      Ast0.offset = lstart.Ast0.pos_info.Ast0.offset; } in
  let info =
    { Ast0.pos_info = pos_info;
      Ast0.whitespace = lstart.Ast0.whitespace;
      Ast0.attachable_start =
      check_attachable (if !inherit_attachable then astart else false);
      Ast0.attachable_end =
      check_attachable (if !inherit_attachable then aend else false);
      Ast0.mcode_start = start_mcodes;
      Ast0.mcode_end = end_mcodes;
      (* only for tokens, not inherited upwards *)
      Ast0.strings_before = []; Ast0.strings_after = [];
      Ast0.isSymbolIdent = false; } in
  {x with Ast0.node = e; Ast0.info = info}

(* --------------------------------------------------------------------- *)

let get_option fn = function
    None -> None
  | Some x -> Some (fn x)

(* --------------------------------------------------------------------- *)
(* --------------------------------------------------------------------- *)
(* Mcode *)

let promote_mcode (_,_,info,mcodekind,_,_) =
  let new_info =
    {info with
      Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind]} in
  {(Ast0.wrap ()) with Ast0.info = new_info; Ast0.mcodekind = ref mcodekind}

let set_mcode_info (a,b,_,c,d,e) info = (a,b,info,c,d,e)

let promote_mcode_plus_one (_,_,info,mcodekind,_,_) =
  let new_pos_info =
    {info.Ast0.pos_info with
      Ast0.line_start = info.Ast0.pos_info.Ast0.line_start + 1;
      Ast0.logical_start = info.Ast0.pos_info.Ast0.logical_start + 1;
      Ast0.line_end = info.Ast0.pos_info.Ast0.line_end + 1;
      Ast0.logical_end = info.Ast0.pos_info.Ast0.logical_end + 1; } in
  let new_info =
    {info with
      Ast0.pos_info = new_pos_info;
      Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind]} in
  {(Ast0.wrap ()) with Ast0.info = new_info; Ast0.mcodekind = ref mcodekind}

let promote_to_statement stm mcodekind =
  let info = Ast0.get_info stm in
  let new_pos_info =
    {info.Ast0.pos_info with
      Ast0.logical_start = info.Ast0.pos_info.Ast0.logical_end;
      Ast0.line_start = info.Ast0.pos_info.Ast0.line_end; } in
  let new_info =
    {info with
      Ast0.pos_info = new_pos_info;
      Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind];
      Ast0.attachable_start = check_attachable true;
      Ast0.attachable_end = check_attachable true} in
  {(Ast0.wrap ()) with Ast0.info = new_info; Ast0.mcodekind = ref mcodekind}

let promote_to_statement_start stm mcodekind =
  let info = Ast0.get_info stm in
  let new_pos_info =
    {info.Ast0.pos_info with
      Ast0.logical_end = info.Ast0.pos_info.Ast0.logical_start;
      Ast0.line_end = info.Ast0.pos_info.Ast0.line_start; } in
  ({info with
     Ast0.pos_info = new_pos_info;
     Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind];
     Ast0.attachable_start = check_attachable true;
     Ast0.attachable_end = check_attachable true;
     Ast0.strings_after = []},
   Ast0.set_info stm {info with Ast0.strings_before = []})

let promote_to_statement_end stm mcodekind =
  let info = Ast0.get_info stm in
  let new_pos_info =
    {info.Ast0.pos_info with
      Ast0.logical_start = info.Ast0.pos_info.Ast0.logical_end;
      Ast0.line_start = info.Ast0.pos_info.Ast0.line_end; } in
  let new_info =
    {info with
     Ast0.pos_info = new_pos_info;
     Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind];
     Ast0.attachable_start = check_attachable true;
     Ast0.attachable_end = check_attachable true;
     Ast0.strings_before = []} in
  (new_info,
   {(Ast0.wrap ()) with Ast0.info = new_info; Ast0.mcodekind = ref mcodekind},
   Ast0.set_info stm {info with Ast0.strings_after = []})

let promote_to_statement_end_mcode (a,ar,info,mc,al,adj) mcodekind =
  let new_pos_info =
    {info.Ast0.pos_info with
      Ast0.logical_end = info.Ast0.pos_info.Ast0.logical_start;
      Ast0.line_end = info.Ast0.pos_info.Ast0.line_start; } in
  let new_info =
    {info with
     Ast0.pos_info = new_pos_info;
     Ast0.mcode_start = [mcodekind]; Ast0.mcode_end = [mcodekind];
     Ast0.attachable_start = check_attachable true;
     Ast0.attachable_end = check_attachable true;
     Ast0.strings_before = []} in
  (new_info,
   {(Ast0.wrap ()) with Ast0.info = new_info; Ast0.mcodekind = ref mcodekind},
   (a,ar,{info with Ast0.strings_after = []},mc,al,adj))

(* mcode is good by default *)
let bad_mcode (t,a,info,mcodekind,pos,adj) =
  let new_info =
    {info with
      Ast0.attachable_start = check_attachable false;
      Ast0.attachable_end = check_attachable false} in
  (t,a,new_info,mcodekind,pos,adj)

let normal_mcode (t,a,info,mcodekind,pos,adj) =
  let new_info =
    if !in_nest_count > 0
    then
      {info with
	Ast0.attachable_start = check_attachable false;
	Ast0.attachable_end = check_attachable false}
    else info in
  (t,a,new_info,mcodekind,pos,adj)

let get_all_start_info l =
  (List.for_all (function x -> (Ast0.get_info x).Ast0.attachable_start) l,
   List.concat (List.map (function x -> (Ast0.get_info x).Ast0.mcode_start) l))

let get_all_end_info l =
  (List.for_all (function x -> (Ast0.get_info x).Ast0.attachable_end) l,
   List.concat (List.map (function x -> (Ast0.get_info x).Ast0.mcode_end) l))

(* --------------------------------------------------------------------- *)
(* Dots *)

(* for the logline classification and the mcode field, on both sides, skip
over initial minus dots, as they don't contribute anything *)
let dot_list is_dots fn = function
    [] -> failwith "dots should not be empty"
  | l ->
      let get_node l fn =
	let first = List.hd l in
	let chosen =
	  match (is_dots first, l) with (true,_::x::_) -> x | _ -> first in
	(* get the logline decorator and the mcodekind of the chosen node *)
	fn (Ast0.get_info chosen) in
      let forward = List.map fn l in
      let backward = List.rev forward in
      let (first_attachable,first_mcode) =
	get_node forward
	  (function x -> (x.Ast0.attachable_start,x.Ast0.mcode_start)) in
      let (last_attachable,last_mcode) =
	get_node backward
	  (function x -> (x.Ast0.attachable_end,x.Ast0.mcode_end)) in
      let first = List.hd forward in
      let last = List.hd backward in
      let first_info =
	{ (Ast0.get_info first) with
	  Ast0.attachable_start = check_attachable first_attachable;
	  Ast0.mcode_start = first_mcode } in
      let last_info =
	{ (Ast0.get_info last) with
	  Ast0.attachable_end = check_attachable last_attachable;
	  Ast0.mcode_end = last_mcode } in
      let first = Ast0.set_info first first_info in
      let last = Ast0.set_info last last_info in
      (forward,first,last)

let dots is_dots prev fn d =
  match (prev,Ast0.unwrap d) with
    (Some prev,[]) -> mkres d [] prev prev
  | (None,[]) ->
      Ast0.set_info d
	{(Ast0.get_info d)
	with
	  Ast0.attachable_start = check_attachable false;
	  Ast0.attachable_end = check_attachable false}
  | (_,x) ->
      let (l,lstart,lend) = dot_list is_dots fn x in
      mkres d l lstart lend

(* --------------------------------------------------------------------- *)
(* Disjunctions *)

let do_disj e starter xs mids ender processor rebuilder =
  let starter = bad_mcode starter in
  let xs = List.map processor xs in
  let mids = List.map bad_mcode mids in
  let ender = bad_mcode ender in
  mkmultires e (rebuilder starter xs mids ender)
    (promote_mcode starter) (promote_mcode ender)
    (get_all_start_info xs) (get_all_end_info xs)

(* --------------------------------------------------------------------- *)
(* Identifier *)

(* for #define name, with no value, to compute right side *)
let mkidres a b c d r = (mkres a b c d,r)

let rec full_ident i =
  match Ast0.unwrap i with
    Ast0.Id(nm) ->
      let nm = normal_mcode nm in
      let name = promote_mcode nm in
      mkidres i (Ast0.Id(nm)) name name (Some name)
  | Ast0.MetaId(nm,a,b,c) ->
      let nm = normal_mcode nm in
      let name = promote_mcode nm in
      mkidres i (Ast0.MetaId(nm,a,b,c)) name name (Some name)
  | Ast0.MetaFunc(nm,a,b) ->
      let nm = normal_mcode nm in
      let name = promote_mcode nm in
      mkidres i (Ast0.MetaFunc(nm,a,b)) name name (Some name)
  | Ast0.MetaLocalFunc(nm,a,b) ->
      let nm = normal_mcode nm in
      let name = promote_mcode nm in
      mkidres i (Ast0.MetaLocalFunc(nm,a,b)) name name (Some name)
  | Ast0.DisjId(starter,ids,mids,ender) ->
      let res =
	do_disj i starter ids mids ender ident
	  (fun starter ids mids ender ->
	    Ast0.DisjId(starter,ids,mids,ender)) in
      (res,None)
  | Ast0.ConjId(starter,ids,mids,ender) ->
      let res =
	do_disj i starter ids mids ender ident
	  (fun starter ids mids ender ->
	    Ast0.ConjId(starter,ids,mids,ender)) in
      (res,None)
  | Ast0.OptIdent(id) ->
      let (id,r) = full_ident id in mkidres i (Ast0.OptIdent(id)) id id r
  | Ast0.AsIdent(id,asid) ->
      let (id,r) = full_ident id in mkidres i (Ast0.AsIdent(id,asid)) id id r
and ident i = let (id,_) = full_ident i in id

(* --------------------------------------------------------------------- *)
(* Expression *)

let is_exp_dots e =
  match Ast0.unwrap e with
    Ast0.Edots(_,_) -> true
  | _ -> false

let is_str_dots e =
  match Ast0.unwrap e with
    Ast0.Strdots(_) -> true
  | _ -> false

let assignOp op =
  let (newop, promoted) = match Ast0.unwrap op with
    | Ast0.SimpleAssign op0 ->
      let op1 = normal_mcode op0 in
      let op2 = promote_mcode op1 in
      ( (Ast0.SimpleAssign op1), op2)
    | Ast0.OpAssign op0 ->
      let op1 = normal_mcode op0 in
      let op2 = promote_mcode op1 in
      ( (Ast0.OpAssign op1), op2)
    | Ast0.MetaAssign (mv0, c, pure) ->
      let mv1 = normal_mcode mv0 in
      let mv2 = promote_mcode mv1 in
      ( Ast0.MetaAssign(mv1, c, pure), mv2) in
  mkres op newop promoted promoted

let binaryOp op =
  let (newop, promoted) = match Ast0.unwrap op with
    | Ast0.Arith op0 ->
      let op1 = normal_mcode op0 in
      let op2 = promote_mcode op1 in
      ( (Ast0.Arith op1), op2)
    | Ast0.Logical op0 ->
      let op1 = normal_mcode op0 in
      let op2 = promote_mcode op1 in
      ( (Ast0.Logical op1), op2)
    | Ast0.MetaBinary (mv0, c, pure) ->
      let mv1 = normal_mcode mv0 in
      let mv2 = promote_mcode mv1 in
      ( Ast0.MetaBinary(mv1, c, pure), mv2) in
  mkres op newop promoted promoted

let rec expression e =
  match Ast0.unwrap e with
    Ast0.Ident(id) ->
      let id = ident id in
      mkres e (Ast0.Ident(id)) id id
  | Ast0.Constant(const) ->
      let const = normal_mcode const in
      let ln = promote_mcode const in
      mkres e (Ast0.Constant(const)) ln ln
  | Ast0.StringConstant(lq,str,rq) ->
      let lq = normal_mcode lq in
      let str =
	dots is_str_dots (Some(promote_mcode lq)) string_fragment str in
      let rq = normal_mcode rq in
      mkres e (Ast0.StringConstant(lq,str,rq))
	(promote_mcode lq) (promote_mcode rq)
  | Ast0.FunCall(fn,lp,args,rp) ->
      let fn = expression fn in
      let lp = normal_mcode lp in
      let args = dots is_exp_dots (Some(promote_mcode lp)) expression args in
      let rp = normal_mcode rp in
      mkres e (Ast0.FunCall(fn,lp,args,rp)) fn (promote_mcode rp)
  | Ast0.Assignment(left,op,right,simple) ->
      let left = expression left in
      let op = assignOp op in
      let right = expression right in
      mkres e (Ast0.Assignment(left,op,right,simple)) left right
  | Ast0.Sequence(left,op,right) ->
      let left = expression left in
      let op = normal_mcode op in
      let right = expression right in
      mkres e (Ast0.Sequence(left,op,right)) left right
  | Ast0.CondExpr(exp1,why,exp2,colon,exp3) ->
      let exp1 = expression exp1 in
      let why = normal_mcode why in
      let exp2 = get_option expression exp2 in
      let colon = normal_mcode colon in
      let exp3 = expression exp3 in
      mkres e (Ast0.CondExpr(exp1,why,exp2,colon,exp3)) exp1 exp3
  | Ast0.Postfix(exp,op) ->
      let exp = expression exp in
      let op = normal_mcode op in
      mkres e (Ast0.Postfix(exp,op)) exp (promote_mcode op)
  | Ast0.Infix(exp,op) ->
      let exp = expression exp in
      let op = normal_mcode op in
      mkres e (Ast0.Infix(exp,op)) (promote_mcode op) exp
  | Ast0.Unary(exp,op) ->
      let exp = expression exp in
      let op = normal_mcode op in
      mkres e (Ast0.Unary(exp,op)) (promote_mcode op) exp
  | Ast0.Binary(left,op,right) ->
      let left = expression left in
      let op = binaryOp op in
      let right = expression right in
      mkres e (Ast0.Binary(left,op,right)) left right
  | Ast0.Nested(left,op,right) ->
      let left = expression left in
      let op = binaryOp op in
      let right = expression right in
      mkres e
        (Ast0.Nested(left,op,right)) left right
  | Ast0.Paren(lp,exp,rp) ->
      let lp = normal_mcode lp in
      let rp = normal_mcode rp in
      mkres e (Ast0.Paren(lp,expression exp,rp))
	(promote_mcode lp) (promote_mcode rp)
  | Ast0.ArrayAccess(exp1,lb,exp2,rb) ->
      let exp1 = expression exp1 in
      let lb = normal_mcode lb in
      let exp2 = expression exp2 in
      let rb = normal_mcode rb in
      mkres e (Ast0.ArrayAccess(exp1,lb,exp2,rb)) exp1 (promote_mcode rb)
  | Ast0.RecordAccess(exp,pt,field) ->
      let exp = expression exp in
      let pt = normal_mcode pt in
      let field = ident field in
      mkres e (Ast0.RecordAccess(exp,pt,field)) exp field
  | Ast0.RecordPtAccess(exp,ar,field) ->
      let exp = expression exp in
      let ar = normal_mcode ar in
      let field = ident field in
      mkres e (Ast0.RecordPtAccess(exp,ar,field)) exp field
  | Ast0.Cast(lp,ty,rp,exp) ->
      let lp = normal_mcode lp in
      let exp = expression exp in
      let rp = normal_mcode rp in
      mkres e (Ast0.Cast(lp,typeC ty,rp,exp)) (promote_mcode lp) exp
  | Ast0.SizeOfExpr(szf,exp) ->
      let szf = normal_mcode szf in
      let exp = expression exp in
      mkres e (Ast0.SizeOfExpr(szf,exp)) (promote_mcode szf) exp
  | Ast0.SizeOfType(szf,lp,ty,rp) ->
      let szf = normal_mcode szf in
      let lp = normal_mcode lp in
      let rp = normal_mcode rp in
      mkres e (Ast0.SizeOfType(szf,lp,typeC ty,rp))
        (promote_mcode szf)  (promote_mcode rp)
  | Ast0.TypeExp(ty) ->
      let ty = typeC ty in mkres e (Ast0.TypeExp(ty)) ty ty
  | Ast0.Constructor(lp,ty,rp,init) ->
      let lp = normal_mcode lp in
      let init = initialiser init in
      let rp = normal_mcode rp in
      mkres e (Ast0.Constructor(lp,typeC ty,rp,init)) (promote_mcode lp) init
  | Ast0.MetaErr(name,a,b) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres e (Ast0.MetaErr(name,a,b)) ln ln
  | Ast0.MetaExpr(name,a,b,c,d,f) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres e (Ast0.MetaExpr(name,a,b,c,d,f)) ln ln
  | Ast0.MetaExprList(name,a,b,c) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres e (Ast0.MetaExprList(name,a,b,c)) ln ln
  | Ast0.EComma(cm) ->
      let cm = normal_mcode cm in
      let ln = promote_mcode cm in
      mkres e (Ast0.EComma(cm)) ln ln
  | Ast0.DisjExpr(starter,exps,mids,ender) ->
      do_disj e starter exps mids ender expression
	(fun starter exps mids ender -> Ast0.DisjExpr(starter,exps,mids,ender))
  | Ast0.ConjExpr(starter,exps,mids,ender) ->
      do_disj e starter exps mids ender expression
	(fun starter exps mids ender -> Ast0.ConjExpr(starter,exps,mids,ender))
  | Ast0.NestExpr(starter,exp_dots,ender,whencode,multi) ->
      (* See explanation on Nest *)
	let wrapper f =
	  match Ast0.get_mcode_mcodekind starter with
	    Ast0.MINUS _ ->
	      in_nest_count := !in_nest_count + 1;
	      let res = f() in
	      in_nest_count := !in_nest_count - 1;
	      res
	  | _ -> f() in
      let exp_dots =
	wrapper (function _ -> dots is_exp_dots None expression exp_dots) in
      let starter = bad_mcode starter in
      let ender = bad_mcode ender in
      mkres e (Ast0.NestExpr(starter,exp_dots,ender,whencode,multi))
	(promote_mcode starter) (promote_mcode ender)
  | Ast0.Edots(dots,whencode) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres e (Ast0.Edots(dots,whencode)) ln ln
  | Ast0.OptExp(exp) ->
      let exp = expression exp in
      mkres e (Ast0.OptExp(exp)) exp exp
  | Ast0.AsExpr(exp,asexp) ->
      let exp = expression exp in
      mkres e (Ast0.AsExpr(exp,asexp)) exp exp
  | Ast0.AsSExpr(exp,asexp) ->
      let exp = expression exp in
      mkres e (Ast0.AsSExpr(exp,asexp)) exp exp

and expression_dots x = dots is_exp_dots None expression x

and string_fragment e =
  match Ast0.unwrap e with
    Ast0.ConstantFragment(str) ->
      let str = normal_mcode str in
      let ln = promote_mcode str in
      mkres e (Ast0.ConstantFragment(str)) ln ln
  | Ast0.FormatFragment(pct,fmt) ->
      let pct = normal_mcode pct in
      let ln = promote_mcode pct in
      let fmt = string_format fmt in
      mkres e (Ast0.FormatFragment(pct,fmt)) ln fmt
  | Ast0.Strdots dots ->
      let dots = normal_mcode dots in
      let ln = promote_mcode dots in
      mkres e (Ast0.Strdots dots) ln ln
  | Ast0.MetaFormatList(pct,name,cstr,lenname) ->
      (* not sure what to do about the following comment... *)
      (* pct is particularly bad in this case, because it is ignored in
	 the matching process.  The metavariable matches the complete format
	 specification, including the % *)
      let pct = normal_mcode pct in
      let ln1 = promote_mcode pct in
      let name = normal_mcode name in
      let ln2 = promote_mcode name in
      mkres e (Ast0.MetaFormatList(pct,name,cstr,lenname)) ln1 ln2

and string_format e =
  match Ast0.unwrap e with
    Ast0.ConstantFormat(str) ->
      let str = normal_mcode str in
      let ln = promote_mcode str in
      mkres e (Ast0.ConstantFormat str) ln ln
  | Ast0.MetaFormat(name,constraints) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres e (Ast0.MetaFormat(name,constraints)) ln ln

(* --------------------------------------------------------------------- *)
(* Types *)

and typeC t =
  match Ast0.unwrap t with
    Ast0.ConstVol(cv,ty) ->
      let cv = normal_mcode cv in
      let ty = typeC ty in
      mkres t (Ast0.ConstVol(cv,ty)) (promote_mcode cv) ty
  | Ast0.BaseType(ty,strings) ->
      let strings = List.map normal_mcode strings in
      let first = List.hd strings in
      let last = List.hd (List.rev strings) in
      mkres t (Ast0.BaseType(ty,strings))
	(promote_mcode first) (promote_mcode last)
  | Ast0.Signed(sgn,None) ->
      let sgn = normal_mcode sgn in
      mkres t (Ast0.Signed(sgn,None)) (promote_mcode sgn) (promote_mcode sgn)
  | Ast0.Signed(sgn,Some ty) ->
      let sgn = normal_mcode sgn in
      let ty = typeC ty in
      mkres t (Ast0.Signed(sgn,Some ty)) (promote_mcode sgn) ty
  | Ast0.Pointer(ty,star) ->
      let ty = typeC ty in
      let star = normal_mcode star in
      mkres t (Ast0.Pointer(ty,star)) ty (promote_mcode star)
  | Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2) ->
      let ty = typeC ty in
      let lp1 = normal_mcode lp1 in
      let star = normal_mcode star in
      let rp1 = normal_mcode rp1 in
      let lp2 = normal_mcode lp2 in
      let params = parameter_list (Some(promote_mcode lp2)) params in
      let rp2 = normal_mcode rp2 in
      mkres t (Ast0.FunctionPointer(ty,lp1,star,rp1,lp2,params,rp2))
	ty (promote_mcode rp2)
  | Ast0.Array(ty,lb,size,rb) ->
      let ty = typeC ty in
      let lb = normal_mcode lb in
      let rb = normal_mcode rb in
      mkres t (Ast0.Array(ty,lb,get_option expression size,rb))
	ty (promote_mcode rb)
  | Ast0.Decimal(dec,lp,length,comma,precision_opt,rp) ->
      let dec = normal_mcode dec in
      let lp = normal_mcode lp in
      let length = expression length in
      let comma = get_option normal_mcode comma in
      let precision_opt = get_option expression precision_opt in
      let rp = normal_mcode rp in
      mkres t (Ast0.Decimal(dec,lp,length,comma,precision_opt,rp))
	(promote_mcode dec) (promote_mcode rp)
  | Ast0.EnumName(kind,Some name) ->
      let kind = normal_mcode kind in
      let name = ident name in
      mkres t (Ast0.EnumName(kind,Some name)) (promote_mcode kind) name
  | Ast0.EnumName(kind,None) ->
      let kind = normal_mcode kind in
      let mc = promote_mcode kind in
      mkres t (Ast0.EnumName(kind,None)) mc mc
  | Ast0.EnumDef(ty,lb,ids,rb) ->
      let ty = typeC ty in
      let lb = normal_mcode lb in
      let ids = dots is_exp_dots (Some(promote_mcode lb)) expression ids in
      let rb = normal_mcode rb in
      mkres t (Ast0.EnumDef(ty,lb,ids,rb)) ty (promote_mcode rb)
  | Ast0.StructUnionName(kind,Some name) ->
      let kind = normal_mcode kind in
      let name = ident name in
      mkres t (Ast0.StructUnionName(kind,Some name)) (promote_mcode kind) name
  | Ast0.StructUnionName(kind,None) ->
      let kind = normal_mcode kind in
      let mc = promote_mcode kind in
      mkres t (Ast0.StructUnionName(kind,None)) mc mc
  | Ast0.StructUnionDef(ty,lb,decls,rb) ->
      let ty = typeC ty in
      let lb = normal_mcode lb in
      let decls =
	dots is_field_dots (Some(promote_mcode lb)) field decls in
      let rb = normal_mcode rb in
      mkres t (Ast0.StructUnionDef(ty,lb,decls,rb)) ty (promote_mcode rb)
  | Ast0.TypeOfExpr(tf,lp,exp,rp) ->
      let tf = normal_mcode tf in
      let lp = normal_mcode lp in
      let exp = expression exp in
      let rp = normal_mcode rp in
      mkres t (Ast0.TypeOfExpr(tf,lp,exp,rp))
	(promote_mcode tf) (promote_mcode rp)
  | Ast0.TypeOfType(tf,lp,ty,rp) ->
      let tf = normal_mcode tf in
      let lp = normal_mcode lp in
      let rp = normal_mcode rp in
      mkres t (Ast0.TypeOfType(tf,lp,typeC ty,rp))
        (promote_mcode tf) (promote_mcode rp)
  | Ast0.TypeName(name) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres t (Ast0.TypeName(name)) ln ln
  | Ast0.MetaType(name,cstr,a) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres t (Ast0.MetaType(name,cstr,a)) ln ln
  | Ast0.DisjType(starter,types,mids,ender) ->
      do_disj t starter types mids ender typeC
	(fun starter types mids ender ->
	  Ast0.DisjType(starter,types,mids,ender))
  | Ast0.ConjType(starter,types,mids,ender) ->
      do_disj t starter types mids ender typeC
	(fun starter types mids ender ->
	  Ast0.ConjType(starter,types,mids,ender))
  | Ast0.OptType(ty) ->
      let ty = typeC ty in mkres t (Ast0.OptType(ty)) ty ty
  | Ast0.AsType _ -> failwith "not possible"

(* --------------------------------------------------------------------- *)
(* Variable declaration *)
(* Even if the Cocci program specifies a list of declarations, they are
   split out into multiple declarations of a single variable each. *)

and declaration d =
  match Ast0.unwrap d with
    Ast0.MetaDecl(name,a,b) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres d (Ast0.MetaDecl(name,a,b)) ln ln
  | Ast0.Init(stg,ty,id,attr,eq,exp,sem) ->
      let ty = typeC ty in
      let id = ident id in
      let attr = List.map normal_mcode attr in
      let eq = normal_mcode eq in
      let exp = initialiser exp in
      let sem = normal_mcode sem in
      (match stg with
	None ->
	  mkres d (Ast0.Init(stg,ty,id,attr,eq,exp,sem)) ty (promote_mcode sem)
      | Some x ->
	  let stg = Some (normal_mcode x) in
	  mkres d (Ast0.Init(stg,ty,id,attr,eq,exp,sem))
	    (promote_mcode x) (promote_mcode sem))
  | Ast0.UnInit(stg,ty,id,attr,sem) ->
      let ty = typeC ty in
      let id = ident id in
      let attr = List.map normal_mcode attr in
      let sem = normal_mcode sem in
      (match stg with
	None ->
	  mkres d (Ast0.UnInit(stg,ty,id,attr,sem)) ty (promote_mcode sem)
      | Some x ->
	  let stg = Some (normal_mcode x) in
	  mkres d (Ast0.UnInit(stg,ty,id,attr,sem))
	    (promote_mcode x) (promote_mcode sem))
  | Ast0.FunProto(fninfo,name,lp1,params,va1,rp1,sem) ->
      let fninfo =
	List.map
	  (function Ast0.FType(ty) -> Ast0.FType(typeC ty) | x -> x)
	  fninfo in
      let name = ident name in
      let lp1 = normal_mcode lp1 in
      let params = parameter_list (Some(promote_mcode lp1)) params in
      let va1 = match va1 with
        | None -> None
        | Some (c1,e1) -> Some (normal_mcode c1, normal_mcode e1) in
      let rp1 = normal_mcode rp1 in
      let sem = normal_mcode sem in
      let res = Ast0.FunProto(fninfo,name,lp1,params,va1,rp1,sem) in
      let right = promote_mcode sem in
      (match fninfo with
	  [] -> mkres d res name right
	| Ast0.FStorage(stg)::_ -> mkres d res (promote_mcode stg) right
	| Ast0.FType(ty)::_ -> mkres d res ty right
	| Ast0.FInline(inline)::_ -> mkres d res (promote_mcode inline) right
	| Ast0.FAttr(attr)::_ -> mkres d res (promote_mcode attr) right)
  | Ast0.MacroDecl(stg,name,lp,args,rp,sem) ->
      let name = ident name in
      let lp = normal_mcode lp in
      let args = dots is_exp_dots (Some(promote_mcode lp)) expression args in
      let rp = normal_mcode rp in
      let sem = normal_mcode sem in
      (match stg with
	None ->
	  mkres d (Ast0.MacroDecl(None,name,lp,args,rp,sem))
	    name (promote_mcode sem)
      | Some x ->
	  let stg = Some (normal_mcode x) in
	  mkres d (Ast0.MacroDecl(stg,name,lp,args,rp,sem))
	    (promote_mcode x) (promote_mcode sem))
  | Ast0.MacroDeclInit(stg,name,lp,args,rp,eq,ini,sem) ->
      let name = ident name in
      let lp = normal_mcode lp in
      let args = dots is_exp_dots (Some(promote_mcode lp)) expression args in
      let rp = normal_mcode rp in
      let eq = normal_mcode eq in
      let ini = initialiser ini in
      let sem = normal_mcode sem in
      (match stg with
	None ->
	  mkres d (Ast0.MacroDeclInit(None,name,lp,args,rp,eq,ini,sem))
	    name (promote_mcode sem)
      | Some x ->
	  let stg = Some (normal_mcode x) in
	  mkres d (Ast0.MacroDeclInit(stg,name,lp,args,rp,eq,ini,sem))
	    (promote_mcode x) (promote_mcode sem))
  | Ast0.TyDecl(ty,sem) ->
      let ty = typeC ty in
      let sem = normal_mcode sem in
      mkres d (Ast0.TyDecl(ty,sem)) ty (promote_mcode sem)
  | Ast0.Typedef(stg,ty,id,sem) ->
      let stg = normal_mcode stg in
      let ty = typeC ty in
      let id = typeC id in
      let sem = normal_mcode sem in
      mkres d (Ast0.Typedef(stg,ty,id,sem))
	(promote_mcode stg) (promote_mcode sem)
  | Ast0.DisjDecl(starter,decls,mids,ender) ->
      do_disj d starter decls mids ender declaration
	(fun starter decls mids ender ->
	  Ast0.DisjDecl(starter,decls,mids,ender))
  | Ast0.ConjDecl(starter,decls,mids,ender) ->
      do_disj d starter decls mids ender declaration
	(fun starter decls mids ender ->
	  Ast0.ConjDecl(starter,decls,mids,ender))
  | Ast0.OptDecl(decl) ->
      let decl = declaration decl in
      mkres d (Ast0.OptDecl(declaration decl)) decl decl
  | Ast0.AsDecl _ -> failwith "not possible"

(* --------------------------------------------------------------------- *)
(* Field declaration *)

and is_field_dots s =
  match Ast0.unwrap s with
    Ast0.Fdots(_,_) -> true
  | _ -> false

and field d =
  match Ast0.unwrap d with
     Ast0.MetaField(name,a,b) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres d (Ast0.MetaField(name,a,b)) ln ln
  | Ast0.MetaFieldList(name,a,b,c) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres d (Ast0.MetaFieldList(name,a,b,c)) ln ln
  | Ast0.Field(ty,id,bf,sem) ->
      let ty = typeC ty in
      let id = Common.map_option ident id in
      let bitfield (c, e) = (normal_mcode c, expression e) in
      let bf = Common.map_option bitfield bf in
      let sem = normal_mcode sem in
      mkres d (Ast0.Field(ty,id,bf,sem)) ty (promote_mcode sem)
  | Ast0.DisjField(starter,decls,mids,ender) ->
      do_disj d starter decls mids ender field
	(fun starter decls mids ender ->
	  Ast0.DisjField(starter,decls,mids,ender))
  | Ast0.ConjField(starter,decls,mids,ender) ->
      do_disj d starter decls mids ender field
	(fun starter decls mids ender ->
	  Ast0.ConjField(starter,decls,mids,ender))
  | Ast0.OptField(decl) ->
      let decl = field decl in
      mkres d (Ast0.OptField(field decl)) decl decl
  | Ast0.Fdots(dots,whencode) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres d (Ast0.Fdots(dots,whencode)) ln ln

(* --------------------------------------------------------------------- *)
(* Initializer *)

and is_init_dots i =
  match Ast0.unwrap i with
    Ast0.Idots(_,_) -> true
  | _ -> false

and initialiser i =
  match Ast0.unwrap i with
    Ast0.MetaInit(name,a,b) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres i (Ast0.MetaInit(name,a,b)) ln ln
  | Ast0.MetaInitList(name,a,b,c) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres i (Ast0.MetaInitList(name,a,b,c)) ln ln
  | Ast0.InitExpr(exp) ->
      let exp = expression exp in
      mkres i (Ast0.InitExpr(exp)) exp exp
  | Ast0.InitList(lb,initlist,rb,ordered) ->
      let lb = normal_mcode lb in
      let initlist =
	dots is_init_dots (Some(promote_mcode lb)) initialiser initlist in
      let rb = normal_mcode rb in
      mkres i (Ast0.InitList(lb,initlist,rb,ordered))
	(promote_mcode lb) (promote_mcode rb)
  | Ast0.InitGccExt(designators,eq,ini) ->
      let (delims,designators) = (* non empty due to parsing *)
	List.split (List.map designator designators) in
      let eq = normal_mcode eq in
      let ini = initialiser ini in
      mkres i (Ast0.InitGccExt(designators,eq,ini))
	(promote_mcode (List.hd delims)) ini
  | Ast0.InitGccName(name,eq,ini) ->
      let name = ident name in
      let eq = normal_mcode eq in
      let ini = initialiser ini in
      mkres i (Ast0.InitGccName(name,eq,ini)) name ini
  | Ast0.IComma(cm) ->
      let cm = normal_mcode cm in
      let ln = promote_mcode cm in
      mkres i (Ast0.IComma(cm)) ln ln
  | Ast0.Idots(dots,whencode) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres i (Ast0.Idots(dots,whencode)) ln ln
  | Ast0.OptIni(ini) ->
      let ini = initialiser ini in
      mkres i (Ast0.OptIni(ini)) ini ini
  | Ast0.AsInit _ -> failwith "not possible"

and designator = function
    Ast0.DesignatorField(dot,id) ->
      let dot = normal_mcode dot in
      (dot,Ast0.DesignatorField(dot,ident id))
  | Ast0.DesignatorIndex(lb,exp,rb) ->
      let lb = normal_mcode lb in
      let rb = normal_mcode rb in
      (lb,Ast0.DesignatorIndex(lb,expression exp,rb))
  | Ast0.DesignatorRange(lb,min,dots,max,rb) ->
      let lb = normal_mcode lb in
      let dots = normal_mcode dots in
      let rb = normal_mcode rb in
      (lb,Ast0.DesignatorRange(lb,expression min,dots,expression max,rb))

and initialiser_list prev = dots is_init_dots prev initialiser

(* for export *)
and initialiser_dots x = dots is_init_dots None initialiser x

(* --------------------------------------------------------------------- *)
(* Parameter *)

and is_param_dots p =
  match Ast0.unwrap p with
    Ast0.Pdots(_) -> true
  | _ -> false

and parameterTypeDef p =
  match Ast0.unwrap p with
    Ast0.VoidParam(ty) ->
      let ty = typeC ty in mkres p (Ast0.VoidParam(ty)) ty ty
  | Ast0.Param(ty,Some id) ->
      let id = ident id in
      let ty = typeC ty in mkres p (Ast0.Param(ty,Some id)) ty id
  | Ast0.Param(ty,None) ->
      let ty = typeC ty in mkres p (Ast0.Param(ty,None)) ty ty
  | Ast0.MetaParam(name,a,b) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres p (Ast0.MetaParam(name,a,b)) ln ln
  | Ast0.MetaParamList(name,a,b,c) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres p (Ast0.MetaParamList(name,a,b,c)) ln ln
  | Ast0.PComma(cm) ->
      let cm = normal_mcode cm in
      let ln = promote_mcode cm in
      mkres p (Ast0.PComma(cm)) ln ln
  | Ast0.Pdots(dots) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres p (Ast0.Pdots(dots)) ln ln
  | Ast0.OptParam(param) ->
      let res = parameterTypeDef param in
      mkres p (Ast0.OptParam(res)) res res
  | Ast0.AsParam _ -> failwith "not possible"

and parameter_list prev = dots is_param_dots prev parameterTypeDef

(* for export *)
let parameter_dots x = dots is_param_dots None parameterTypeDef x

(* --------------------------------------------------------------------- *)

let is_define_param_dots s =
  match Ast0.unwrap s with
    Ast0.DPdots(_) -> true
  | _ -> false

let rec define_param p =
  match Ast0.unwrap p with
    Ast0.DParam(id) ->
      let id = ident id in mkres p (Ast0.DParam(id)) id id
  | Ast0.MetaDParamList(name,a,b,c) ->
      let name = normal_mcode name in
      let ln = promote_mcode name in
      mkres p (Ast0.MetaDParamList(name,a,b,c)) ln ln
  | Ast0.DPComma(cm) ->
      let cm = normal_mcode cm in
      let ln = promote_mcode cm in
      mkres p (Ast0.DPComma(cm)) ln ln
  | Ast0.DPdots(dots) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres p (Ast0.DPdots(dots)) ln ln
  | Ast0.OptDParam(dp) ->
      let res = define_param dp in
      mkres p (Ast0.OptDParam(res)) res res

let define_parameters x id =
  match Ast0.unwrap x with
    Ast0.NoParams -> (x,id) (* no info, should be ignored *)
  | Ast0.DParams(lp,dp,rp) ->
      let lp = normal_mcode lp in
      let dp = dots is_define_param_dots None define_param dp in
      let rp = normal_mcode rp in
      let l = promote_mcode lp in
      let r = promote_mcode rp in
      (mkres x (Ast0.DParams(lp,dp,rp)) l r, r)

(* --------------------------------------------------------------------- *)
(* Top-level code *)

let is_stm_dots s =
  match Ast0.unwrap s with
    Ast0.Dots(_,_) -> true
  | _ -> false

let is_ec_dots e =
  match Ast0.unwrap e with
    Ast0.ExecDots(_) -> true
  | _ -> false

let rec statement s =
  let res =
    match Ast0.unwrap s with
      Ast0.Decl((info,bef),decl) ->
	let decl = declaration decl in
	let (leftinfo,decl) = promote_to_statement_start decl bef in
	let leftinfo =
	  (* for function prototypes, which may have information placed here
	     before calling Compute_lines *)
	  {leftinfo with Ast0.strings_before = info.Ast0.strings_before} in
	mkres s (Ast0.Decl((leftinfo,bef),decl)) decl decl
    | Ast0.Seq(lbrace,body,rbrace) ->
	let lbrace = normal_mcode lbrace in
	let body =
	  dots is_stm_dots (Some(promote_mcode lbrace)) statement body in
	let rbrace = normal_mcode rbrace in
	mkres s (Ast0.Seq(lbrace,body,rbrace))
	  (promote_mcode lbrace) (promote_mcode rbrace)
    | Ast0.ExprStatement(Some exp,sem) ->
	let exp = expression exp in
	let sem = normal_mcode sem in
	mkres s (Ast0.ExprStatement(Some exp,sem)) exp (promote_mcode sem)
    | Ast0.ExprStatement(None,sem) ->
	let sem = normal_mcode sem in
	let promoted_sem = promote_mcode sem in
	mkres s (Ast0.ExprStatement(None,sem)) promoted_sem promoted_sem
    | Ast0.IfThen(iff,lp,exp,rp,branch,(_,aft,adj)) ->
	let iff = normal_mcode iff in
	let lp = normal_mcode lp in
	let exp = expression exp in
	let rp = normal_mcode rp in
	let branch = statement branch in
	let (rightinfo,right,branch) = promote_to_statement_end branch aft in
	mkres s
	  (Ast0.IfThen(iff,lp,exp,rp,branch,(rightinfo,aft,adj)))
	  (promote_mcode iff) right
    | Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,(_,aft,adj)) ->
	let iff = normal_mcode iff in
	let lp = normal_mcode lp in
	let exp = expression exp in
	let rp = normal_mcode rp in
	let branch1 = statement branch1 in
	let els = normal_mcode els in
	let branch2 = statement branch2 in
	let (rightinfo,right,branch2) = promote_to_statement_end branch2 aft in
	mkres s
	  (Ast0.IfThenElse(iff,lp,exp,rp,branch1,els,branch2,
	    (rightinfo,aft,adj)))
	  (promote_mcode iff) right
    | Ast0.While(wh,lp,exp,rp,body,(_,aft,adj)) ->
	let wh = normal_mcode wh in
	let lp = normal_mcode lp in
	let exp = expression exp in
	let rp = normal_mcode rp in
	let body = statement body in
	let (rightinfo,right,body) = promote_to_statement_end body aft in
	mkres s (Ast0.While(wh,lp,exp,rp,body,(rightinfo,aft,adj)))
	  (promote_mcode wh) right
    | Ast0.Do(d,body,wh,lp,exp,rp,sem) ->
	let d = normal_mcode d in
	let body = statement body in
	let wh = normal_mcode wh in
	let lp = normal_mcode lp in
	let exp = expression exp in
	let rp = normal_mcode rp in
	mkres s (Ast0.Do(d,body,wh,lp,exp,rp,sem))
	  (promote_mcode d) (promote_mcode sem)
    | Ast0.For(fr,lp,first,exp2,sem2,exp3,rp,body,(_,aft,adj)) ->
	let fr = normal_mcode fr in
	let lp = normal_mcode lp in
	let first =
	  match Ast0.unwrap first with
	    Ast0.ForExp(None,sem1) ->
	      let sem1 = normal_mcode sem1 in
	      mkres first (Ast0.ForExp(None,sem1))
		(promote_mcode sem1) (promote_mcode sem1)
	  | Ast0.ForExp(Some exp1,sem1) ->
	      let exp1 = expression exp1 in
	      let sem1 = normal_mcode sem1 in
	      mkres first (Ast0.ForExp(Some exp1,sem1))
		exp1 (promote_mcode sem1)
	  | Ast0.ForDecl((_,bef),decl) ->
	      let decl = declaration decl in
	      let (leftinfo,decl) = promote_to_statement_start decl bef in
	      mkres first (Ast0.ForDecl ((leftinfo,bef),decl)) decl decl in
	let exp2 = get_option expression exp2 in
	let sem2 = normal_mcode sem2 in
	let exp3 = get_option expression exp3 in
	let rp = normal_mcode rp in
	let body = statement body in
	let (rightinfo,right,body) = promote_to_statement_end body aft in
	mkres s (Ast0.For(fr,lp,first,exp2,sem2,exp3,rp,body,
			  (rightinfo,aft,adj)))
	  (promote_mcode fr) right
    | Ast0.Iterator(nm,lp,args,rp,body,(_,aft,adj)) ->
	let nm = ident nm in
	let lp = normal_mcode lp in
	let args = dots is_exp_dots (Some(promote_mcode lp)) expression args in
	let rp = normal_mcode rp in
	let body = statement body in
	let (rightinfo,right,body) = promote_to_statement_end body aft in
	mkres s
	  (Ast0.Iterator(nm,lp,args,rp,body,(rightinfo,aft,adj)))
	  nm right
    | Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb) ->
	let switch = normal_mcode switch in
	let lp = normal_mcode lp in
	let exp = expression exp in
	let rp = normal_mcode rp in
	let lb = normal_mcode lb in
	let decls =
	  dots is_stm_dots (Some(promote_mcode lb))
	    statement decls in
	let cases =
	  dots (function _ -> false)
	    (if Ast0.unwrap decls = []
	    then (Some(promote_mcode lb))
	    else None (* not sure this is right, but not sure the case can
			 arise either *))
	    case_line cases in
	let rb = normal_mcode rb in
	mkres s
	  (Ast0.Switch(switch,lp,exp,rp,lb,decls,cases,rb))
	  (promote_mcode switch) (promote_mcode rb)
    | Ast0.Break(br,sem) ->
	let br = normal_mcode br in
	let sem = normal_mcode sem in
	mkres s (Ast0.Break(br,sem)) (promote_mcode br) (promote_mcode sem)
    | Ast0.Continue(cont,sem) ->
	let cont = normal_mcode cont in
	let sem = normal_mcode sem in
	mkres s (Ast0.Continue(cont,sem))
	  (promote_mcode cont) (promote_mcode sem)
    | Ast0.Label(l,dd) ->
	let l = ident l in
	let dd = normal_mcode dd in
	mkres s (Ast0.Label(l,dd)) l (promote_mcode dd)
    | Ast0.Goto(goto,id,sem) ->
	let goto = normal_mcode goto in
	let id = ident id in
	let sem = normal_mcode sem in
	mkres s (Ast0.Goto(goto,id,sem))
	  (promote_mcode goto) (promote_mcode sem)
    | Ast0.Return(ret,sem) ->
	let ret = normal_mcode ret in
	let sem = normal_mcode sem in
	mkres s (Ast0.Return(ret,sem)) (promote_mcode ret) (promote_mcode sem)
    | Ast0.ReturnExpr(ret,exp,sem) ->
	let ret = normal_mcode ret in
	let exp = expression exp in
	let sem = normal_mcode sem in
	mkres s (Ast0.ReturnExpr(ret,exp,sem))
	  (promote_mcode ret) (promote_mcode sem)
    | Ast0.Exec(exec,lang,code,sem) ->
	let exec = normal_mcode exec in
	let lang = normal_mcode lang in
	let code = dots is_ec_dots (Some(promote_mcode lang)) exec_code code in
	let sem = normal_mcode sem in
	mkres s (Ast0.Exec(exec,lang,code,sem))
	  (promote_mcode exec) (promote_mcode sem)
    | Ast0.MetaStmt(name,a,b) ->
	let name = normal_mcode name in
	let ln = promote_mcode name in
	mkres s (Ast0.MetaStmt(name,a,b)) ln ln
    | Ast0.MetaStmtList(name,a,b,c) ->
	(* This becomes ..., which becomes a metavar.  The metavar, with
	   any attachments that this has, is then iterated over the code.
	   If we attach anything here, then it will be duplicated for each
	   statement in the list. *)
	let name = bad_mcode name in
	let ln = promote_mcode name in
	mkres s (Ast0.MetaStmtList(name,a,b,c)) ln ln
    | Ast0.Exp(exp) ->
	let exp = expression exp in
	mkres s (Ast0.Exp(exp)) exp exp
    | Ast0.TopExp(exp) ->
	let exp = expression exp in
	mkres s (Ast0.TopExp(exp)) exp exp
    | Ast0.Ty(ty) ->
	let ty = typeC ty in
	mkres s (Ast0.Ty(ty)) ty ty
    | Ast0.TopId(id) ->
	let id = ident id in
	mkres s (Ast0.TopId(id)) id id
    | Ast0.TopInit(init) ->
	let init = initialiser init in
	mkres s (Ast0.TopInit(init)) init init
    | Ast0.Disj(starter,rule_elem_dots_list,mids,ender) ->
	let starter = bad_mcode starter in
	let mids = List.map bad_mcode mids in
	let ender = bad_mcode ender in
	let rec loop prevs = function
	    [] -> []
	  | stm::stms ->
	      (dots is_stm_dots (Some(promote_mcode_plus_one(List.hd prevs)))
		 statement stm)::
	      (loop (List.tl prevs) stms) in
	let elems = loop (starter::mids) rule_elem_dots_list in
	mkmultires s (Ast0.Disj(starter,elems,mids,ender))
	  (promote_mcode starter) (promote_mcode ender)
	  (get_all_start_info elems) (get_all_end_info elems)
    | Ast0.Conj(starter,rule_elem_dots_list,mids,ender) ->
	let starter = bad_mcode starter in
	let mids = List.map bad_mcode mids in
	let ender = bad_mcode ender in
	let rec loop prevs = function
	    [] -> []
	  | stm::stms ->
	      (dots is_stm_dots (Some(promote_mcode_plus_one(List.hd prevs)))
		 statement stm)::
	      (loop (List.tl prevs) stms) in
	let elems = loop (starter::mids) rule_elem_dots_list in
	mkmultires s (Ast0.Conj(starter,elems,mids,ender))
	  (promote_mcode starter) (promote_mcode ender)
	  (get_all_start_info elems) (get_all_end_info elems)
    | Ast0.Nest(starter,rule_elem_dots,ender,whencode,multi) ->
	let starter = bad_mcode starter in
	let ender = bad_mcode ender in
	let wrapper f =
	  match Ast0.get_mcode_mcodekind starter with
	    Ast0.MINUS _ ->
	      (* if minus, then all nest code has to be minus.  This is
		 checked at the token level, in parse_cocci.ml.  All nest code
		 is also unattachable.  We strip the minus annotations from
		 the nest code because in the CTL another metavariable will
		 take care of removing all the code matched by the nest.
		 Without stripping the minus annotations, we would get a
		 double transformation.  Perhaps there is a more elegant
		 way to do this in the CTL, but it is not easy, because of
		 the interaction with the whencode and the implementation of
		 plus *)
	      in_nest_count := !in_nest_count + 1;
	      let res = f() in
	      in_nest_count := !in_nest_count - 1;
	      res
	  | _ -> f() in
	let rule_elem_dots =
	  wrapper
	    (function _ -> dots is_stm_dots None statement rule_elem_dots) in
	mkres s (Ast0.Nest(starter,rule_elem_dots,ender,whencode,multi))
	  (promote_mcode starter) (promote_mcode ender)
    | Ast0.Dots(dots,whencode) ->
	let dots = bad_mcode dots in
	let ln = promote_mcode dots in
	mkres s (Ast0.Dots(dots,whencode)) ln ln
    | Ast0.FunDecl((_,bef),fninfo,name,lp,params,va,rp,lbrace,body,rbrace,
		   (_,aft)) ->
	let fninfo =
	  List.map
	    (function Ast0.FType(ty) -> Ast0.FType(typeC ty) | x -> x)
	    fninfo in
	let name = ident name in
	let lp = normal_mcode lp in
	let params = parameter_list (Some(promote_mcode lp)) params in
	let rp = normal_mcode rp in
	let lbrace = normal_mcode lbrace in
	let body =
	  dots is_stm_dots (Some(promote_mcode lbrace)) statement body in
	let rbrace = normal_mcode rbrace in
	let (leftinfo,fninfo,name) = leftfninfo fninfo name bef in
	let (rightinfo,right,rbrace) =
	  promote_to_statement_end_mcode rbrace aft in
      (* pretend it is one line before the start of the function, so that it
	 will catch things defined at top level.  We assume that these will not
	 be defined on the same line as the function.  This is a HACK.
	 A better approach would be to attach top_level things to this node,
	 and other things to the node after, but that would complicate
	 insert_plus, which doesn't distinguish between different mcodekinds *)
	let res =
	  Ast0.FunDecl((leftinfo,bef),fninfo,name,lp,params,va,rp,lbrace,
		       body,rbrace,(rightinfo,aft)) in
      (* have to do this test again, because of typing problems - can't save
	 the result, only use it *)
	(match fninfo with
	  [] -> mkres s res name right
	| Ast0.FStorage(stg)::_ -> mkres s res (promote_mcode stg) right
	| Ast0.FType(ty)::_ -> mkres s res ty right
	| Ast0.FInline(inline)::_ -> mkres s res (promote_mcode inline) right
	| Ast0.FAttr(attr)::_ -> mkres s res (promote_mcode attr) right)

    | Ast0.Include(inc,stm) ->
	let inc = normal_mcode inc in
	let stm = normal_mcode stm in
	mkres s (Ast0.Include(inc,stm)) (promote_mcode inc) (promote_mcode stm)
    | Ast0.MetaInclude(inc,s) ->
	let inc = normal_mcode inc in
	let s = expression s in
	mkres s (Ast0.MetaInclude(inc,s)) (promote_mcode inc) s

    | Ast0.Undef(def,id) ->
	let def = normal_mcode def in
	let id = ident id in
	mkres s (Ast0.Undef(def,id)) (promote_mcode def) id
    | Ast0.Define(def,id,params,body) ->
	let def = normal_mcode def in
	let (id,right) = full_ident id in
	(match right with
	  None -> failwith "no disj id for #define"
	| Some right ->
	    let (params,prev) = define_parameters params right in
	    let body = dots is_stm_dots (Some prev) statement body in
	    mkres s (Ast0.Define(def,id,params,body)) (promote_mcode def) body)
    | Ast0.Pragma(prg,id,body) ->
	let prg = normal_mcode prg in
	let id = ident id in
	let body = pragmainfo body in
	mkres s (Ast0.Pragma(prg,id,body)) (promote_mcode prg) body
    | Ast0.OptStm(stm) ->
	let stm = statement stm in mkres s (Ast0.OptStm(stm)) stm stm
    | Ast0.AsStmt(stm,asstm) ->
	let stm = statement stm in mkres s (Ast0.AsStmt(stm,asstm)) stm stm in
  Ast0.set_dots_bef_aft res
    (match Ast0.get_dots_bef_aft res with
      Ast0.NoDots -> Ast0.NoDots
    | Ast0.AddingBetweenDots s ->
	Ast0.AddingBetweenDots(statement s)
    | Ast0.DroppingBetweenDots s ->
	Ast0.DroppingBetweenDots(statement s))

and leftfninfo fninfo name bef = (* cases on what is leftmost *)
  match fninfo with
    [] ->
      let (leftinfo,name) = promote_to_statement_start name bef in
      (leftinfo,[],name)
  | Ast0.FStorage(stg)::rest ->
      let (leftinfo,stginfo) =
	promote_to_statement_start (promote_mcode stg) bef in
      (leftinfo,
       Ast0.FStorage(set_mcode_info stg (Ast0.get_info stginfo))::rest,
       name)
  | Ast0.FType(ty)::rest ->
      let (leftinfo,ty) = promote_to_statement_start ty bef in
      (leftinfo,Ast0.FType(ty)::rest,name)
  | Ast0.FInline(inline)::rest ->
      let (leftinfo,inlinfo) =
	promote_to_statement_start (promote_mcode inline) bef in
      (leftinfo,
       Ast0.FInline(set_mcode_info inline (Ast0.get_info inlinfo))::rest,
       name)
  | Ast0.FAttr(attr)::rest ->
      let (leftinfo,attrinfo) =
	promote_to_statement_start (promote_mcode attr) bef in
      (leftinfo,
       Ast0.FAttr(set_mcode_info attr (Ast0.get_info attrinfo))::rest,
       name)

and pragmainfo pi =
  match Ast0.unwrap pi with
    Ast0.PragmaString(s) ->
      let s = bad_mcode s in
      let ln = promote_mcode s in
      mkres pi (Ast0.PragmaString(s)) ln ln
  | Ast0.PragmaDots(dots) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres pi (Ast0.PragmaDots(dots)) ln ln

and case_line c =
  match Ast0.unwrap c with
    Ast0.Default(def,colon,code) ->
      let def = normal_mcode def in
      let colon = normal_mcode colon in
      let code = dots is_stm_dots (Some(promote_mcode colon)) statement code in
      mkres c (Ast0.Default(def,colon,code)) (promote_mcode def) code
  | Ast0.Case(case,exp,colon,code) ->
      let case = normal_mcode case in
      let exp = expression exp in
      let colon = normal_mcode colon in
      let code = dots is_stm_dots (Some(promote_mcode colon)) statement code in
      mkres c (Ast0.Case(case,exp,colon,code)) (promote_mcode case) code
  | Ast0.DisjCase(starter,case_lines,mids,ender) ->
      do_disj c starter case_lines mids ender case_line
	(fun starter case_lines mids ender ->
	  Ast0.DisjCase(starter,case_lines,mids,ender))
  | Ast0.OptCase(case) ->
      let case = case_line case in mkres c (Ast0.OptCase(case)) case case

and exec_code e =
  match Ast0.unwrap e with
    Ast0.ExecEval(colon,id) ->
      let colon = normal_mcode colon in
      let id = expression id in
      mkres e (Ast0.ExecEval(colon,id)) (promote_mcode colon) id
  | Ast0.ExecToken(tok) ->
      let tok = normal_mcode tok in
      let ln = promote_mcode tok in
      mkres e (Ast0.ExecToken tok) ln ln
  | Ast0.ExecDots(dots) ->
      let dots = bad_mcode dots in
      let ln = promote_mcode dots in
      mkres e (Ast0.ExecDots dots) ln ln

and statement_dots x = dots is_stm_dots None statement x

(* --------------------------------------------------------------------- *)
(* Function declaration *)

let top_level t =
  match Ast0.unwrap t with
    Ast0.FILEINFO(old_file,new_file) -> t
  | Ast0.NONDECL(stmt) ->
      let stmt = statement stmt in mkres t (Ast0.NONDECL(stmt)) stmt stmt
  | Ast0.CODE(rule_elem_dots) ->
      let rule_elem_dots = dots is_stm_dots None statement rule_elem_dots in
      mkres t (Ast0.CODE(rule_elem_dots)) rule_elem_dots rule_elem_dots
  | Ast0.ERRORWORDS(exps) -> t
  | Ast0.OTHER(_) | Ast0.TOPCODE(_) -> failwith "eliminated by top_level"

(* --------------------------------------------------------------------- *)
(* Entry points *)

let compute_lines attachable_or x =
  in_nest_count := 0;
  inherit_attachable := attachable_or;
  List.map top_level x

let compute_statement_lines attachable_or x =
  in_nest_count := 0;
  inherit_attachable := attachable_or;
  statement x

let compute_statement_dots_lines attachable_or x =
  in_nest_count := 0;
  inherit_attachable := attachable_or;
  statement_dots x