cil-formatparse.opp.exp 50.8 KB
Newer Older
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
File "cil-formatparse.mly", line 259, characters 14-21:
Warning: the token AND_AND is unused.
File "cil-formatparse.mly", line 274, characters 24-27:
Warning: the token ASM is unused.
File "cil-formatparse.mly", line 244, characters 45-49:
Warning: the token AUTO is unused.
File "cil-formatparse.mly", line 276, characters 7-21:
Warning: the token BLOCKATTRIBUTE is unused.
File "cil-formatparse.mly", line 275, characters 7-21:
Warning: the token BUILTIN_VA_ARG is unused.
File "cil-formatparse.mly", line 275, characters 22-37:
Warning: the token BUILTIN_VA_LIST is unused.
File "cil-formatparse.mly", line 267, characters 14-18:
Warning: the token CASE is unused.
File "cil-formatparse.mly", line 305, characters 48-52:
Warning: the token CAST is unused.
File "cil-formatparse.mly", line 264, characters 7-12:
Warning: the token COLON is unused.
File "cil-formatparse.mly", line 233, characters 16-24:
Warning: the token CST_CHAR is unused.
File "cil-formatparse.mly", line 235, characters 16-25:
Warning: the token CST_FLOAT is unused.
File "cil-formatparse.mly", line 236, characters 16-26:
Warning: the token CST_STRING is unused.
File "cil-formatparse.mly", line 237, characters 16-27:
Warning: the token CST_WSTRING is unused.
File "cil-formatparse.mly", line 277, characters 7-15:
Warning: the token DECLSPEC is unused.
File "cil-formatparse.mly", line 267, characters 19-26:
Warning: the token DEFAULT is unused.
File "cil-formatparse.mly", line 268, characters 13-15:
Warning: the token DO is unused.
File "cil-formatparse.mly", line 242, characters 7-11:
Warning: the token ENUM is unused.
File "cil-formatparse.mly", line 240, characters 7-10:
Warning: the token EOF is unused.
File "cil-formatparse.mly", line 244, characters 16-22:
Warning: the token EXTERN is unused.
File "cil-formatparse.mly", line 268, characters 16-19:
Warning: the token FOR is unused.
File "cil-formatparse.mly", line 274, characters 35-45:
Warning: the token FUNCTION__ is unused.
File "cil-formatparse.mly", line 266, characters 22-26:
Warning: the token GOTO is unused.
File "cil-formatparse.mly", line 274, characters 17-23:
Warning: the token INLINE is unused.
File "cil-formatparse.mly", line 241, characters 40-45:
Warning: the token INT32 is unused.
File "cil-formatparse.mly", line 241, characters 34-39:
Warning: the token INT64 is unused.
File "cil-formatparse.mly", line 274, characters 64-71:
Warning: the token LABEL__ is unused.
File "cil-formatparse.mly", line 261, characters 17-28:
Warning: the token MINUS_MINUS is unused.
File "cil-formatparse.mly", line 278, characters 16-21:
Warning: the token MSASM is unused.
File "cil-formatparse.mly", line 278, characters 22-28:
Warning: the token MSATTR is unused.
File "cil-formatparse.mly", line 238, characters 16-26:
Warning: the token NAMED_TYPE is unused.
File "cil-formatparse.mly", line 259, characters 22-31:
Warning: the token PIPE_PIPE is unused.
File "cil-formatparse.mly", line 261, characters 7-16:
Warning: the token PLUS_PLUS is unused.
File "cil-formatparse.mly", line 279, characters 7-13:
Warning: the token PRAGMA is unused.
File "cil-formatparse.mly", line 274, characters 46-63:
Warning: the token PRETTY_FUNCTION__ is unused.
File "cil-formatparse.mly", line 264, characters 38-43:
Warning: the token QUEST is unused.
File "cil-formatparse.mly", line 244, characters 50-58:
Warning: the token REGISTER is unused.
File "cil-formatparse.mly", line 243, characters 7-13:
Warning: the token SIGNED is unused.
File "cil-formatparse.mly", line 244, characters 23-29:
Warning: the token STATIC is unused.
File "cil-formatparse.mly", line 267, characters 7-13:
Warning: the token SWITCH is unused.
File "cil-formatparse.mly", line 269, characters 10-14:
Warning: the token THEN is unused.
File "cil-formatparse.mly", line 242, characters 19-26:
Warning: the token TYPEDEF is unused.
83 84
File "cil-formatparse.mly", line 338, characters 0-10:
Warning: symbol initialize is unreachable from any of the start symbol(s).
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 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
%{
open Cil
open Pretty
module E = Errormsg

let parse_error msg : 'a =           (* sm: c++-mode highlight hack: -> ' <- *)
  E.hadErrors := true;
  E.parse_error
    msg
    (Parsing.symbol_start ()) (Parsing.symbol_end ())


let getArg (argname: string) (args: (string * formatArg) list) = 
  try 
    snd (List.find (fun (n, a) -> n = argname) args)
  with _ -> 
    E.s (error "Pattern string %s does not have argument with name %s\n"
           !Lexerhack.currentPattern argname)

let wrongArgType (which: string) (expected: string) (found: formatArg) = 
  E.s (bug "Expecting %s argument (%s) and found %a\n" 
         expected which d_formatarg found)

let doUnop (uo: unop) subexp = 
  ((fun args -> 
        let e = (fst subexp) args in
        UnOp(uo, e, typeOf e)),

   (fun e -> match e with 
     UnOp(uo', e', _) when uo  = uo' -> (snd subexp) e' 
   | _ -> None)) 

let buildPlus e1 e2 : exp = 
  let t1 = typeOf e1 in
  if isPointerType t1 then 
    BinOp(PlusPI, e1, e2, t1)
  else 
    BinOp(PlusA, e1, e2, t1)

let buildMinus e1 e2 : exp = 
  let t1 = typeOf e1 in
  let t2 = typeOf e2 in
  if isPointerType t1 then
    if isPointerType t2 then
      BinOp(MinusPP, e1, e2, intType)
    else
      BinOp(MinusPI, e1, e2, t1)
  else
    BinOp(MinusA, e1, e2, t1)

let doBinop bop e1t e2t = 
  ((fun args -> 
    let e1 = (fst e1t) args in 
    let e2 = (fst e2t) args in 
    let t1 = typeOf e1 in
    BinOp(bop, e1, e2, t1)),

   (fun e -> match e with 
     BinOp(bop', e1, e2, _) when bop' = bop -> begin
       match (snd e1t) e1, (snd e2t) e2 with
         Some m1, Some m2 -> Some (m1 @ m2)
       | _, _ -> None
     end
   | _ -> None))

(* Check the equivalence of two format lists *)
let rec checkSameFormat (fl1: formatArg list) (fl2: formatArg list) = 
  match fl1, fl2 with 
    [], [] -> true
  | h1::t1, h2::t2 -> begin
      let rec checkOffsetEq o1 o2 = 
        match o1, o2 with
          NoOffset, NoOffset -> true
        | Field(f1, o1'), Field(f2, o2') -> 
            f1.fname = f2.fname && checkOffsetEq o1' o2'
        | Index(e1, o1'), Index(e2, o2') -> 
            checkOffsetEq o1' o2' && checkExpEq e1 e2
        | _, _ -> false

      and checkExpEq e1 e2 = 
        match e1, e2 with 
          Const(CInt64(n1, _, _)), Const(CInt64(n2, _, _)) -> n1 = n2
        | Lval l1, Lval l2 -> checkLvalEq l1 l2
        | UnOp(uo1, e1, _), UnOp(uo2, e2, _) -> 
            uo1 = uo2 && checkExpEq e1 e2
        | BinOp(bo1, e11, e12, _), BinOp(bo2, e21, e22, _) -> 
            bo1 = bo2 && checkExpEq e11 e21 && checkExpEq e21 e22
        | AddrOf l1, AddrOf l2 -> checkLvalEq l1 l2
        | StartOf l1, StartOf l2 -> checkLvalEq l1 l2
        | SizeOf t1, SizeOf t2 -> typeSig t1 = typeSig t2
        | _, _ -> 
            ignore (E.warn "checkSameFormat for Fe"); false

      and checkLvalEq l1 l2 = 
        match l1, l2 with
          (Var v1, o1), (Var v2, o2) -> v1 == v2 && checkOffsetEq o1 o2
        | (Mem e1, o1), (Mem e2, o2) -> 
            checkOffsetEq o1 o2 && checkExpEq e1 e2
        | _, _ -> false
      in
      let hdeq = 
        match h1, h2 with 
          Fv v1, Fv v2 -> v1 == v2
        | Fd n1, Fd n2 -> n1 = n2
        | Fe e1, Fe e2 -> checkExpEq e1 e2
        | Fi i1, Fi i2 -> ignore (E.warn "checkSameFormat for Fi"); false
        | Ft t1, Ft t2 -> typeSig t1 = typeSig t2
        | Fl l1, Fl l2 -> checkLvalEq l1 l2
        | Fo o1, Fo o2 -> checkOffsetEq o1 o2
        | Fc c1, Fc c2 -> c1 == c2
        | _, _ -> false
      in
      hdeq || checkSameFormat t1 t2
  end
  | _, _ -> false

let matchBinopEq (bopeq: binop -> bool) lvt et = 
  (fun i -> match i with 
    Set (lv, BinOp(bop', Lval (lv'), e', _), l) when bopeq bop' -> begin
      match lvt lv, lvt lv', et e' with 
        Some m1, Some m1', Some m2 -> 
          (* Must check that m1 and m2 are the same *)
          if checkSameFormat m1 m1' then 
            Some (m1 @ m2)
          else
            None
      | _, _, _ -> None
     end
  | _ -> None)

let doBinopEq bop lvt et = 
  ((fun loc args -> 
    let l = (fst lvt) args in
    Set(l, BinOp(bop, (Lval l), (fst et) args, typeOfLval l), loc)),

   matchBinopEq (fun bop' -> bop = bop') (snd lvt) (snd et))


let getField (bt: typ) (fname: string) : fieldinfo = 
  match unrollType bt with 
    TComp(ci, _) -> begin
      try
        List.find (fun f -> fname = f.fname) ci.cfields
      with Not_found -> 
        E.s (bug "Cannot find field %s in %s\n" fname (compFullName ci))
    end
  | t -> E.s (bug "Trying to access field %s in non-struct\n" fname) 


let matchIntType (ik: ikind) (t:typ) : formatArg list option = 
  match unrollType t with 
    TInt(ik', _) when ik = ik' -> Some []
  | _ -> None

let matchFloatType (fk: fkind) (t:typ) : formatArg list option = 
  match unrollType t with 
    TFloat(fk', _) when fk = fk' -> Some []
  | _ -> None

let doAttr (id: string) 
           (aargs: (((string * formatArg) list -> attrparam list) * 
                    (attrparam list -> formatArg list option)) option)
    = 
  let t = match aargs with 
    Some t -> t
  | None -> (fun _ -> []), 
            (function [] -> Some [] | _ -> None)
  in
  ((fun args -> Attr (id, (fst t) args)), 
   
   (fun attrs -> 
     (* Find the attributes with the same ID *)
     List.fold_left 
       (fun acc a -> 
         match acc, a with 
           Some _, _ -> acc (* We found one already *)
         | None, Attr(id', args) when id = id' -> 
             (* Now match the arguments *)
             (snd t) args 
         | None, _ -> acc)
       None
       attrs))


type falist = formatArg list

type maybeInit = 
    NoInit
  | InitExp of exp
  | InitCall of lval * exp list

%}
%start expression
%start instr
%start lval
%start offset
%start stmt
%start stmt_list
%start typename
%token WHILE
%token VOID
%token UNSIGNED
%token UNION
%token TYPEOF
%token TYPEDEF
%token THEN
%token SWITCH
%token STRUCT
%token STATIC
%token SIGNED
%token SHORT
%token SEMICOLON
%token RETURN
%token REGISTER
%token RBRACKET
%token RBRACE
%token PRETTY_FUNCTION__
%token PRAGMA
%token <string> NAMED_TYPE
%token <string> MSATTR
%token <string> MSASM
%token LONG
%token LABEL__
%token INT64
%token INT32
%token INT
%token INLINE
%token GOTO
%token FUNCTION__
%token FOR
%token FLOAT
%token EXTERN
%token EOF
%token ENUM
%token ELLIPSIS
%token DOUBLE
%token DO
%token DEFAULT
%token DECLSPEC
%token <string> CST_WSTRING
%token <string> CST_STRING
%token <string> CST_FLOAT
%token <string> CST_CHAR
%token CONTINUE
%token CHAR
%token CASE
%token BUILTIN_VA_LIST
%token BUILTIN_VA_ARG
%token BREAK
%token BLOCKATTRIBUTE
%token AUTO
%token ATTRIBUTE
%token ASM
%token <string> ARG_va
%token <string> ARG_t
%token <string> ARG_s
%token <string> ARG_p
%token <string> ARG_o
%token <string> ARG_k
%token <string> ARG_f
%token <string> ARG_eo
%token <string> ARG_c
%token <string> ARG_S
%token <string> ARG_P
%token <string> ARG_F
%token <string> ARG_E
%token <string> ARG_A
%token IF
%token ELSE
%token COMMA
%token <string> ARG_v
%token <string> ARG_lo
%token <string> ARG_l
%token <string> ARG_i
%token <string> ARG_g
%token <string> ARG_e
%token <string> ARG_d
%token <string> ARG_I
%token SUP_SUP_EQ
%token STAR_EQ
%token SLASH_EQ
%token PLUS_EQ
%token PIPE_EQ
%token PERCENT_EQ
%token MINUS_EQ
%token INF_INF_EQ
%token EQ
%token CIRC_EQ
%token AND_EQ
%token COLON
%token PIPE_PIPE
%token AND_AND
%token <string> ARG_b
%token PIPE
%token CIRC
%token AND
%token EXCLAM_EQ
%token EQ_EQ
%token SUP_EQ
%token SUP
%token INF_EQ
%token INF
%token SUP_SUP
%token INF_INF
%token PLUS
%token MINUS
%token VOLATILE
%token STAR
%token SLASH
%token RESTRICT
%token PERCENT
%token CONST
%token TILDE
%token SIZEOF
%token RPAREN
%token PLUS_PLUS
%token MINUS_MINUS
%token EXCLAM
%token <string> ARG_u
%token ALIGNOF
%token LBRACKET
%token LPAREN
%token LBRACE
%token DOT
%token ARROW
%token QUEST
%token <string> IDENT
%token <string> CST_INT
%nonassoc IF 
%nonassoc ELSE 
%left COMMA 
%nonassoc ARG_v ARG_lo ARG_l ARG_i ARG_g ARG_e ARG_d ARG_I 
%right SUP_SUP_EQ STAR_EQ SLASH_EQ PLUS_EQ PIPE_EQ PERCENT_EQ MINUS_EQ INF_INF_EQ EQ CIRC_EQ AND_EQ 
%right COLON 
%left PIPE_PIPE 
%left AND_AND 
%left ARG_b 
%left PIPE 
%left CIRC 
%left AND 
%left EXCLAM_EQ EQ_EQ 
%left SUP_EQ SUP INF_EQ INF 
%left SUP_SUP INF_INF 
%left PLUS MINUS 
%left VOLATILE STAR SLASH RESTRICT PERCENT CONST 
%right TILDE SIZEOF RPAREN PLUS_PLUS MINUS_MINUS EXCLAM CAST ARG_u ALIGNOF ADDROF 
%left LBRACKET 
%left LPAREN LBRACE DOT ARROW 
%nonassoc QUEST IDENT CST_INT 
%type <((string * Cil.formatArg) list -> Cil.exp) * (Cil.exp -> Cil.formatArg list option)> constant
%type <((string * Cil.formatArg) list -> Cil.exp) * (Cil.exp -> Cil.formatArg list option)> expression
%type <unit> initialize
%type <(Cil.location -> (string * Cil.formatArg) list -> Cil.instr) * (Cil.instr -> Cil.formatArg list option)> instr
%type <((string * Cil.formatArg) list -> Cil.lval) * (Cil.lval -> Cil.formatArg list option)> lval
%type <(Cil.typ -> (string * Cil.formatArg) list -> Cil.offset) * (Cil.offset -> Cil.formatArg list option)> offset
%type <((string * Cil.formatArg) list -> (string * Cil.typ * Cil.attributes) list option * bool) * ((string * Cil.typ * Cil.attributes) list option * bool -> Cil.formatArg list option)> parameters
%type <((string -> Cil.typ -> Cil.varinfo) -> Cil.location -> (string * Cil.formatArg) list -> Cil.stmt)> stmt
%type <((string -> Cil.typ -> Cil.varinfo) -> Cil.location -> (string * Cil.formatArg) list -> Cil.stmt list)> stmt_list
%type <(Cil.attributes -> (string * Cil.formatArg) list -> Cil.typ) * (Cil.typ -> Cil.formatArg list option)> type_spec
%type <((string * Cil.formatArg) list -> Cil.typ) * (Cil.typ -> Cil.formatArg list option)> typename
%%

expression:
| ARG_e
    {  (* Count arguments eagerly *) 
                            let currentArg = $1 in 
                            ((fun args ->
                               match getArg currentArg args with 
                                   Fe e -> e
                                 | a -> wrongArgType currentArg 
                                            "expression" a),

                             (fun e -> Some [ Fe e ]))
                         }
| constant
    { $1 }
| lval %prec IDENT
    { ((fun args -> Lval ((fst $1) args)),

                             (fun e -> match e with 
                                Lval l -> (snd $1) l 
                              | _ -> None))
                         }
| SIZEOF expression
    { ((fun args -> SizeOfE ((fst $2) args)),

                           fun e -> match e with 
                             SizeOfE e' -> (snd $2) e'
                           | _ -> None)
                        }
| SIZEOF LPAREN typename RPAREN
    { ((fun args -> SizeOf ((fst $3) args)),
                          
                           (fun e -> match e with 
                              SizeOf t -> (snd $3) t
                           |  _ -> None))
                        }
| ALIGNOF expression
    { ((fun args -> AlignOfE ((fst $2) args)),

                           (fun e -> match e with
                             AlignOfE e' -> (snd $2) e' | _ -> None))
                        }
| ALIGNOF LPAREN typename RPAREN
    { ((fun args -> AlignOf ((fst $3) args)),

                           (fun e -> match e with
                             AlignOf t' -> (snd $3) t' | _ -> None))
                        }
| PLUS expression
    { $2 }
| MINUS expression
    { doUnop Neg $2 }
| EXCLAM expression
    { doUnop LNot $2 }
| TILDE expression
    { doUnop BNot $2 }
| argu expression %prec ARG_u
    { ((fun args -> 
                             let e = (fst $2) args in
                             UnOp((fst $1) args, e, typeOf e)),

                           (fun e -> match e with 
                             UnOp(uo, e', _) -> begin
                               match (snd $1) uo, (snd $2) e' with 
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _ -> None
                             end
                           | _ -> None))
                        }
| AND expression %prec ADDROF
    { ((fun args -> 
                             match (fst $2) args with
                                Lval l -> mkAddrOf l
                              | _ -> E.s (bug "AddrOf applied to a non lval")),
                          (fun e -> match e with 
                            AddrOf l -> (snd $2) (Lval l)
                          | e -> (snd $2) (Lval (mkMem e NoOffset))))
                         }
| LPAREN expression RPAREN
    { $2 }
| expression PLUS expression
    { ((fun args -> buildPlus ((fst $1) args) 
                                                  ((fst $3) args)), 
                          (fun e -> match e with 
                            BinOp((PlusPI|PlusA), e1, e2, _) -> begin
                              match (snd $1) e1, (snd $3) e2 with
                                Some m1, Some m2 -> Some (m1 @ m2)
                              | _, _ -> None
                            end
                          | _ -> None))
                        }
| expression MINUS expression
    { ((fun args -> buildMinus ((fst $1) args)
                                                   ((fst $3) args)),

                           (fun e -> match e with 
                             BinOp((MinusPP|MinusPI|MinusA), e1, e2, _) -> 
                               begin
                                 match (snd $1) e1, (snd $3) e2 with
                                   Some m1, Some m2 -> Some (m1 @ m2)
                                 | _, _ -> None
                               end
                           | _ -> None))
                        }
| expression argb expression %prec ARG_b
    { ((fun args -> 
                               let e1 = (fst $1) args in 
                               let bop = (fst $2) args in
                               let e2 = (fst $3) args in 
                               let t1 = typeOf e1 in
                               BinOp(bop, e1, e2, t1)),
                           
                           (fun e -> match e with 
                             BinOp(bop, e1, e2, _) -> begin
                               match (snd $1) e1,(snd $2) bop,(snd $3) e2 with
                                 Some m1, Some m2, Some m3 -> 
                                   Some (m1 @ m2 @ m3)
                               | _, _, _ -> None
                             end
                           | _ -> None))
                        }
| expression STAR expression
    { doBinop Mult $1 $3 }
| expression SLASH expression
    { doBinop Div $1 $3 }
| expression PERCENT expression
    { doBinop Mod $1 $3 }
| expression INF_INF expression
    { doBinop Shiftlt $1 $3 }
| expression SUP_SUP expression
    { doBinop Shiftrt $1 $3 }
| expression AND expression
    { doBinop BAnd $1 $3 }
| expression PIPE expression
    { doBinop BOr $1 $3 }
| expression CIRC expression
    { doBinop BXor $1 $3 }
| expression EQ_EQ expression
    { doBinop Eq $1 $3 }
| expression EXCLAM_EQ expression
    { doBinop Ne $1 $3 }
| expression INF expression
    { doBinop Lt $1 $3 }
| expression SUP expression
    { doBinop Gt $1 $3 }
| expression INF_EQ expression
    { doBinop Le $1 $3 }
| expression SUP_EQ expression
    { doBinop Ge $1 $3 }
| LPAREN typename RPAREN expression
    { ((fun args -> 
                              let t = (fst $2) args in
                              let e = (fst $4) args in
                              mkCast e t),
                            
                            (fun e -> 
                              let t', e' = 
                                match e with 
                                  CastE (t', e') -> t', e'
                                | _ -> typeOf e, e
                              in
                              match (snd $2) t', (snd $4 e') with
                                Some m1, Some m2 -> Some (m1 @ m2)
                              | _, _ -> None))
                         }

argu:
| ARG_u
    { let currentArg = $1 in
                         ((fun args ->
                           match getArg currentArg args with
                             Fu uo -> uo
                           | a -> wrongArgType currentArg "unnop" a),

                          fun uo -> Some [ Fu uo ])
                       }

argb:
| ARG_b
    { let currentArg = $1 in
                         ((fun args ->
                           match getArg currentArg args with
                             Fb bo -> bo
                           | a -> wrongArgType currentArg "binop" a),

                          fun bo -> Some [ Fb bo ])
                       }

constant:
| ARG_d
    { let currentArg = $1 in
                           ((fun args ->
                             match getArg currentArg args with
                               Fd n -> integer n
                              | a -> wrongArgType currentArg "integer" a),

                            fun e -> match e with 
                              Const(CInt64(n, _, _)) -> 
                                Some [ Fd (Int64.to_int n) ]
                            | _ -> None) 
                         }
| ARG_g
    { let currentArg = $1 in
                        ((fun args ->
                             match getArg currentArg args with
                               Fg s -> Const(CStr s)
                              | a -> wrongArgType currentArg "string" a),

                            fun e -> match e with 
                              Const(CStr s) ->
                                Some [ Fg s ]
                            | _ -> None) 
                         }
| CST_INT
    { let n = parseInt $1 in
                           ((fun args -> n),

                            (fun e -> match e, n with 
                              Const(CInt64(e', _, _)), 
                              Const(CInt64(n', _, _)) when e' = n' -> Some []
                            | _ -> None))
                         }

lval:
| ARG_l
    { let currentArg = $1 in
                           ((fun args -> 
                                match getArg currentArg args with 
                                  Fl l -> l
                                | Fv v -> Var v, NoOffset
                                | a -> wrongArgType currentArg "lval" a),

                            fun l -> Some [ Fl l ])
                         }
| argv offset %prec ARG_v
    { ((fun args -> 
                              let v = (fst $1) args in
                               (Var v, (fst $2) v.vtype args)),

                            (fun l -> match l with 
                              Var vi, off -> begin
                                match (snd $1) vi, (snd $2) off with 
                                  Some m1, Some m2 -> Some (m1 @ m2)
                                | _ -> None
                              end
                            | _ -> None))
                         }
| STAR expression
    { ((fun args -> mkMem ((fst $2) args) NoOffset),

                           (fun l -> match l with 
                              Mem e, NoOffset -> (snd $2) e
                           | _, _ -> None))
                         }
| expression ARROW IDENT offset
    { ((fun args -> 
                   let e = (fst $1) args in
                   let baset = 
                     match unrollTypeDeep (typeOf e) with 
                       TPtr (t, _) -> t
                     | _ -> E.s (bug "Expecting a pointer for field %s\n" $3)
                   in
                   let fi = getField baset $3 in
                   mkMem e (Field(fi, (fst $4) fi.ftype args))),

                (fun l -> match l with 
                   Mem e, Field(fi, off) when fi.fname = $3 -> begin
                     match (snd $1) e, (snd $4) off with 
                       Some m1, Some m2 -> Some (m1 @ m2)
                     | _, _ -> None
                   end
                | _, _ -> None))
             }
| LPAREN STAR expression RPAREN offset
    { ((fun args -> 
                 let e = (fst $3) args in
                 let baset = 
                   match unrollTypeDeep (typeOf e) with 
                     TPtr (t, _) -> t
                   | _ -> E.s (bug "Expecting a pointer\n")
                 in
                 mkMem e ((fst $5) baset args)),

                (fun l -> match l with 
                  Mem e, off -> begin
                    match (snd $3) e, (snd $5 off) with 
                      Some m1, Some m2 -> Some (m1 @ m2)
                    | _, _ -> None
                  end
                | _, _ -> None))
              }

argv:
| ARG_v
    { let currentArg = $1 in
                         ((fun args ->
                           match getArg currentArg args with
                             Fv v -> v
                           | a -> wrongArgType currentArg "varinfo" a),

                          fun v -> Some [ Fv v ])
                       }
| IDENT
    { let currentArg = $1 in
                         ((fun args -> 
                           match getArg currentArg args with 
                             Fv v -> v
                           | a -> wrongArgType currentArg "varinfo" a),
                         (fun v -> 
                             E.s (bug "identifiers (%s) are not supported for deconstruction" currentArg)))
                       }

offset:
| ARG_o
    { let currentArg = $1 in
                            ((fun t args -> 
                                match getArg currentArg args with 
                                  Fo o -> o
                                | a -> wrongArgType currentArg "offset" a),

                              (fun off -> Some [ Fo off ]))
                          }
| 
    { ((fun t args -> NoOffset),

                             (fun off -> match off with 
                                NoOffset -> Some []
                              | _ -> None))
                          }
| DOT IDENT offset
    { ((fun t args -> 
                                let fi = getField t $2 in
                                Field (fi, (fst $3) fi.ftype args)),

                            (fun off -> match off with 
                               Field (fi, off') when fi.fname = $2 -> 
                                 (snd $3) off'
                            | _ -> None))
                          }
| LBRACKET expression RBRACKET offset
    { ((fun t args -> 
                     let bt = 
                       match unrollType t with 
                         TArray(bt, _, _) -> bt 
                       | _ -> E.s (error "Formatcil: expecting an array for index")
                     in
                     let e = (fst $2) args in 
                     Index(e, (fst $4) bt args)),

                    (fun off -> match off with 
                      Index (e, off') -> begin
                        match (snd $2) e, (snd $4) off with 
                          Some m1, Some m2 -> Some (m1 @ m2)
                        | _, _ -> None
                      end
                    | _ -> None))
                    }

typename:
| one_formal
    { ((fun args -> 
                            let (_, ft, _) = (fst $1) args in
                            ft),

                         (fun t -> (snd $1) ("", t, [])))
                      }

one_formal:
| type_spec attributes decl
    { ((fun args -> 
                        let tal = (fst $2) args in
                        let ts = (fst $1) tal args in
                        let (fn, ft, _) = (fst $3) ts args in
                        (fn, ft, [])),

                      (fun (fn, ft, fa) -> 
                         match (snd $3) (fn, ft) with 
                           Some (restt, m3) -> begin
                             match (snd $1) restt, 
                                   (snd $2) (typeAttrs restt)with
                               Some m1, Some m2 -> 
                                 Some (m1 @ m2 @ m3)
                             | _, _ -> None
                           end
                         | _ -> None))
                   }
| ARG_f
    { let currentArg = $1 in
                     ((fun args -> 
                         match getArg currentArg args with
                          Ff (fn, ft, fa) -> (fn, ft, fa)
                         | a  -> wrongArgType currentArg "formal" a),

                      (fun (fn, ft, fa) -> Some [ Ff (fn, ft, fa) ]))
                   }

type_spec:
| ARG_t
    { let currentArg = $1 in
                     ((fun al args -> 
                       match getArg currentArg args with 
                          Ft t -> typeAddAttributes al t
                       | a -> wrongArgType currentArg "type" a),
                      
                      (fun t -> Some [ Ft t ]))
                      }
| VOID
    { ((fun al args -> TVoid al),

                       (fun t -> match unrollType t with 
                           TVoid _ -> Some []
                         | _ -> None)) }
| ARG_k
    { let currentArg = $1 in
                      ((fun al args -> 
                        match getArg currentArg args with 
                          Fk ik -> TInt(ik, al)
                        | a -> wrongArgType currentArg "ikind" a),

                       (fun t -> match unrollType t with 
                         TInt(ik, _) -> Some [ Fk ik ]
                       | _ -> None))
                    }
| CHAR
    { ((fun al args -> TInt(IChar, al)),
                       (matchIntType IChar)) }
| UNSIGNED CHAR
    { ((fun al args -> TInt(IUChar, al)), 
                       matchIntType IUChar) }
| SHORT
    { ((fun al args -> TInt(IShort, al)), 
                       matchIntType IShort) }
| UNSIGNED SHORT
    { ((fun al args -> TInt(IUShort, al)), 
                       matchIntType IUShort) }
| INT
    { ((fun al args -> TInt(IInt, al)), 
                       matchIntType IInt) }
| UNSIGNED INT
    { ((fun al args -> TInt(IUInt, al)), matchIntType IUInt) }
| LONG
    { ((fun al args -> TInt(ILong, al)), 
                        matchIntType ILong) }
| UNSIGNED LONG
    { ((fun al args -> TInt(IULong, al)), 
                        matchIntType IULong) }
| LONG LONG
    { ((fun al args -> TInt(ILongLong, al)), 
                          
                          matchIntType ILongLong)
                        }
| UNSIGNED LONG LONG
    { ((fun al args -> TInt(IULongLong, al)),

                             matchIntType IULongLong)
                           }
| FLOAT
    { ((fun al args -> TFloat(FFloat, al)),
                       matchFloatType FFloat) 
                    }
| DOUBLE
    { ((fun al args -> TFloat(FDouble, al)),
                       matchFloatType FDouble) }
| STRUCT ARG_c
    { let currentArg = $2 in
                      ((fun al args -> 
                         match getArg currentArg args with 
                           Fc ci -> TComp(ci, al)
                         | a -> wrongArgType currentArg "compinfo" a),

                        (fun t -> match unrollType t with 
                            TComp(ci, _) -> Some [ Fc ci ]
                          | _ -> None))
                    }
| UNION ARG_c
    { let currentArg = $2 in
                     ((fun al args -> 
                         match getArg currentArg args with 
                           Fc ci -> TComp(ci, al)
                         | a -> wrongArgType currentArg "compinfo" a),

                        (fun t -> match unrollType t with 
                            TComp(ci, _) -> Some [ Fc ci ]
                          | _ -> None))

                   }
| TYPEOF LPAREN expression RPAREN
    { ((fun al args -> typeAddAttributes al 
                                        (typeOf ((fst $3) args))),
                    
                      (fun t -> E.s (bug "Cannot match typeof(e)\n")))
                   }

decl:
| STAR attributes decl
    { ((fun ts args -> 
                         let al = (fst $2) args in
                         (fst $3) (TPtr(ts, al)) args),

                       (fun (fn, ft) -> 
                         match (snd $3) (fn, ft) with 
                           Some (TPtr(bt, al), m2) -> begin
                             match (snd $2) al with 
                               Some m1 -> Some (bt, m1 @ m2)
                             | _ -> None
                           end
                         | _ -> None))
                    }
| direct_decl
    { $1 }

direct_decl:
| 
    { ((fun ts args -> ("", ts, [])),

                      (* Match any name in this case *)
                      (fun (fn, ft) -> 
                         Some (unrollType ft, [])))
                   }
| IDENT
    { ((fun ts args -> ($1, ts, [])),

                      (fun (fn, ft) -> 
                        if fn = "" || fn = $1 then 
                          Some (unrollType ft, []) 
                        else 
                          None))
                   }
| LPAREN attributes decl RPAREN
    { ((fun ts args -> 
                          let al = (fst $2) args in
                          (fst $3) (typeAddAttributes al ts) args),

                      (fun (fn, ft) -> begin
                        match (snd $3) (fn, ft) with
                          Some (restt, m2) -> begin
                            match (snd $2) (typeAttrs restt) with 
                              Some m1 -> Some (restt, m1 @ m2)
                            | _ -> None
                          end
                        | _ -> None
                      end))
                   }
| direct_decl LBRACKET exp_opt RBRACKET
    { ((fun ts args -> 
                        (fst $1) (TArray(ts, (fst $3) args, [])) args),

                     (fun (fn, ft) -> 
                       match (snd $1) (fn, ft) with 
                         Some (TArray(bt, lo, _), m1) -> begin
                           match (snd $3) lo with 
                             Some m2 -> Some (unrollType bt, m1 @ m2)
                           | _ -> None
                         end 
                       | _ -> None))
                   }
| LPAREN attributes decl RPAREN LPAREN parameters RPAREN
    { ((fun ts args -> 
                        let al = (fst $2) args in
                        let pars, isva = (fst $6) args in
                        (fst $3) (TFun(ts, pars, isva, al)) args),

                      (fun (fn, ft) -> 
                         match (snd $3) (fn, ft) with 
                           Some (TFun(rt, args, isva, al), m1) -> begin
                             match (snd $2) al, (snd $6) (args, isva) with 
                               Some m2, Some m6 
                               -> Some (unrollType rt, m1 @ m2 @ m6)
                             | _ -> None
                           end
                         | _ -> None))
                   }

parameters:
| 
    { ((fun args -> (None, false)),

                     (* Match any formals *)
                      (fun (pars, isva) -> 
                        match pars, isva with 
                          (_, false) -> Some [] 
                        | _ -> None))
                   }
| parameters_ne
    { ((fun args -> 
                        let (pars : (string * typ * attributes) list), 
                            (isva : bool) = (fst $1) args in
                        (Some pars), isva),

                     (function 
                         ((Some pars), isva) -> (snd $1) (pars, isva)
                       |  _ -> None))
                   }

parameters_ne:
| ELLIPSIS
    { ((fun args -> ([], true)),

                      (function 
                          ([], true) -> Some [] 
                        | _ -> None))
                   }
| ARG_va
    { let currentArg = $1 in
                     ((fun args -> 
                       match getArg currentArg args with
                         Fva isva -> ([], isva)
                       | a -> wrongArgType currentArg "vararg" a),

                     (function 
                         ([], isva) -> Some [ Fva isva ] 
                       | _ -> None))
                   }
| ARG_F
    { let currentArg = $1 in
                     ((fun args -> 
                       match getArg currentArg args with
                        FF fl -> ( fl, false)
                       | a  -> wrongArgType currentArg "formals" a),

                      (function 
                          (pars, false) -> Some [ FF pars ] 
                        | _ -> None))
                   }
| one_formal
    { ((fun args -> ([(fst $1) args], false)),

                     (function 
                         ([ f ], false) -> (snd $1) f 
                       | _ -> None))
                   }
| one_formal COMMA parameters_ne
    { ((fun args -> 
                        let this = (fst $1) args in
                        let (rest, isva) = (fst $3) args in
                        (this :: rest, isva)),

                      (function 
                          ((f::rest, isva)) -> begin
                            match (snd $1) f, (snd $3) (rest, isva) with 
                              Some m1, Some m2 -> Some (m1 @ m2)
                            | _, _ -> None
                          end
                        | _ -> None))
                   }

exp_opt:
| 
    { ((fun args -> None),
                      (* Match anything if the pattern does not have a len *)
                      (fun _ -> Some [])) }
| expression
    { ((fun args -> Some ((fst $1) args)),

                      (fun lo -> match lo with
                        Some e -> (snd $1) e
                      | _ -> None))
                   }
| ARG_eo
    { let currentArg = $1 in
                     ((fun args ->
                       match getArg currentArg args with
                         Feo lo -> lo
                       | a -> wrongArgType currentArg "exp_opt" a),

                      fun lo -> Some [ Feo lo ])
                   }

attributes:
| 
    { ((fun args -> []), 
                     (fun attrs -> Some [])) }
| ARG_A
    { let currentArg = $1 in
                    ((fun args -> 
                        match getArg currentArg args with
                          FA al -> al
                        | a -> wrongArgType currentArg "attributes" a),

                     (fun al -> Some [ FA al ]))
                  }
| attribute attributes
    { ((fun args ->
                       addAttribute ((fst $1) args) ((fst $2) args)),
                     (* Pass all the attributes down *)
                     (fun attrs ->
                       match (snd $1) attrs, (snd $2) attrs with
                         Some m1, Some m2 -> Some (m1 @ m2)
                       | _, _ -> None))
                  }

attribute:
| CONST
    { doAttr "const" None }
| RESTRICT
    { doAttr "restrict" None }
| VOLATILE
    { doAttr "volatile" None }
| ATTRIBUTE LPAREN LPAREN attr RPAREN RPAREN
    { $4 }

attr:
| IDENT
    { doAttr $1 None }
| IDENT LPAREN attr_args_ne RPAREN
    { doAttr $1 (Some $3) }

attr_args_ne:
| attr_arg
    { ((fun args -> [ (fst $1) args ]),

                                    (fun aargs -> match aargs with 
                                      [ arg ] -> (snd $1) arg
                                    | _ -> None))
                                 }
| attr_arg COMMA attr_args_ne
    { ((fun args -> 
                                      let this = (fst $1) args in
                                      this :: ((fst $3) args)),

                                    (fun aargs -> match aargs with 
                                      h :: rest -> begin
                                        match (snd $1) h, (snd $3) rest with
                                          Some m1, Some m2 -> Some (m1 @ m2)
                                        | _, _ -> None
                                      end
                                    | _ -> None))
                                  }
| ARG_P
    { let currentArg = $1 in
                          ((fun args -> 
                            match getArg currentArg args with
                              FP al -> al
                            | a -> wrongArgType currentArg "attrparams" a),

                           (fun al -> Some [ FP al ]))
                        }

attr_arg:
| IDENT
    { ((fun args -> ACons($1, [])),

                        (fun aarg -> match aarg with 
                            ACons(id, []) when id = $1 -> Some []
                        | _ -> None))
                     }
| IDENT LPAREN attr_args_ne RPAREN
    { ((fun args -> ACons($1, (fst $3) args)),

                        (fun aarg -> match aarg with 
                            ACons(id, args) when id = $1 -> 
                              (snd $3) args
                        | _ -> None))
                     }
| ARG_p
    { let currentArg = $1 in
                       ((fun args -> 
                          match getArg currentArg args with
                            Fp p -> p
                          | a -> wrongArgType currentArg "attrparam" a),

                        (fun ap -> Some [ Fp ap]))
                     }

instr:
| ARG_i SEMICOLON
    { let currentArg = $1 in
                          ((fun loc args -> 
                                match getArg currentArg args with 
                                  Fi i -> i
                                | a -> wrongArgType currentArg "instr" a),

                           (fun i -> Some [ Fi i]))
                        }
| lval EQ expression SEMICOLON
    { ((fun loc args -> 
                              Set((fst $1) args, (fst $3) args, loc)),

                           (fun i -> match i with
                             Set (lv, e, l) -> begin
                               match (snd $1) lv, (snd $3) e with
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _, _ -> None
                             end
                           | _ -> None))
                        }
| lval PLUS_EQ expression SEMICOLON
    { ((fun loc args -> 
                              let l = (fst $1) args in
                              Set(l, buildPlus (Lval l) ((fst $3) args), loc)),

                           matchBinopEq 
                             (fun bop -> bop = PlusPI || bop = PlusA)
                             (snd $1) (snd $3)) 
                        }
| lval MINUS_EQ expression SEMICOLON
    { ((fun loc args -> 
                              let l = (fst $1) args in
                              Set(l, 
                                  buildMinus (Lval l) ((fst $3) args), loc)),

                           matchBinopEq (fun bop -> bop = MinusA 
                                               || bop = MinusPP 
                                               || bop = MinusPI) 
                                      (snd $1)  (snd $3)) 
                        }
| lval STAR_EQ expression SEMICOLON
    { doBinopEq Mult $1 $3 }
| lval SLASH_EQ expression SEMICOLON
    { doBinopEq Div $1 $3 }
| lval PERCENT_EQ expression SEMICOLON
    { doBinopEq Mod $1 $3 }
| lval AND_EQ expression SEMICOLON
    { doBinopEq BAnd $1 $3 }
| lval PIPE_EQ expression SEMICOLON
    { doBinopEq BOr $1 $3 }
| lval CIRC_EQ expression SEMICOLON
    { doBinopEq BXor $1 $3 }
| lval INF_INF_EQ expression SEMICOLON
    { doBinopEq Shiftlt $1 $3 }
| lval SUP_SUP_EQ expression SEMICOLON
    { doBinopEq Shiftrt $1 $3 }
| lval EQ lval LPAREN arguments RPAREN SEMICOLON
    { ((fun loc args -> 
                              Call(Some ((fst $1) args), Lval ((fst $3) args), 
                                     (fst $5) args, loc)),

                           (fun i -> match i with 
                             Call(Some l, Lval f, args, loc) -> begin
                               match (snd $1) l, (snd $3) f, (snd $5) args with
                                 Some m1, Some m2, Some m3 -> 
                                   Some (m1 @ m2 @ m3)
                               | _, _, _ -> None
                             end
                           | _ -> None))
                        }
| lval LPAREN arguments RPAREN SEMICOLON
    { ((fun loc args -> 
                              Call(None, Lval ((fst $1) args), 
                                     (fst $3) args, loc)), 

                           (fun i -> match i with 
                             Call(None, Lval f, args, loc) -> begin
                               match (snd $1) f, (snd $3) args with
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _, _ -> None
                             end
                           | _ -> None))
                         }
| arglo lval LPAREN arguments RPAREN SEMICOLON
    { ((fun loc args -> 
                       Call((fst $1) args, Lval ((fst $2) args), 
                            (fst $4) args, loc)), 
                        
                        (fun i -> match i with 
                          Call(lo, Lval f, args, loc) -> begin
                            match (snd $1) lo, (snd $2) f, (snd $4) args with
                              Some m1, Some m2, Some m3 -> 
                                Some (m1 @ m2 @ m3)
                            | _, _, _ -> None
                          end
                        | _ -> None))
                     }

arglo:
| ARG_lo
    { let currentArg = $1 in
                           ((fun args -> 
                             let res = 
                               match getArg currentArg args with
                                 Flo x -> x
                               | a -> wrongArgType currentArg "lval option" a
                             in
                             res),

                            (fun lo -> Some [ Flo lo ]))
                         }

arguments:
| 
    { ((fun args -> []), 

                     (fun actuals -> match actuals with 
                          [] -> Some []
                         | _ -> None))
                  }
| arguments_ne
    { $1 }

arguments_ne:
| expression
    { ((fun args -> [ (fst $1) args ]),

                     (fun actuals -> match actuals with 
                        [ h ] -> (snd $1) h
                       | _ -> None))
                  }
| ARG_E
    {  let currentArg = $1 in
                     ((fun args -> 
                         match getArg currentArg args with
                           FE el -> el
                          | a -> wrongArgType currentArg "arguments" a),

                      (fun actuals -> Some [ FE actuals ]))
                  }
| expression COMMA arguments_ne
    { ((fun args -> ((fst $1) args) :: ((fst $3) args)),

                     (fun actuals -> match actuals with 
                         h :: rest -> begin
                           match (snd $1) h, (snd $3) rest with 
                             Some m1, Some m2 -> Some (m1 @ m2)
                           | _, _ -> None
                         end
                       | _ -> None))
                  }

stmt:
| IF LPAREN expression RPAREN stmt %prec IF
    { (fun mkTemp loc args -> 
                         mkStmt (If((fst $3) args, 
                                    mkBlock [ $5 mkTemp loc args ],
                                    mkBlock [], loc)))
                  }
| IF LPAREN expression RPAREN stmt ELSE stmt
    { (fun mkTemp loc args -> 
                         mkStmt (If((fst $3) args, 
                                    mkBlock [ $5 mkTemp loc args ],
                                    mkBlock [ $7 mkTemp loc args], loc)))
                  }
| RETURN exp_opt SEMICOLON
    { (fun mkTemp loc args -> 
                         mkStmt (Return((fst $2) args, loc))) 
                  }
| BREAK SEMICOLON
    { (fun mkTemp loc args -> 
                         mkStmt (Break loc))
                  }
| CONTINUE SEMICOLON
    { (fun mkTemp loc args -> 
                         mkStmt (Continue loc))
                  }
| LBRACE stmt_list RBRACE
    { (fun mkTemp loc args -> 
                         let stmts = $2 mkTemp loc args in
                         mkStmt (Block (mkBlock (stmts))))
                  }
| WHILE LPAREN expression RPAREN stmt
    { (fun mkTemp loc args -> 
                        let e = (fst $3) args in
                        let e = 
                          if isPointerType(typeOf e) then 
                            mkCast e !upointType
                          else e
                        in
                        mkStmt 
                          (Loop (mkBlock [ mkStmt 
                                             (If(e,
                                                 mkBlock [],
                                                 mkBlock [ mkStmt 
                                                             (Break loc) ],
                                                 loc));
                                           $5 mkTemp loc args ],
                                 loc, None, None)))
                   }
| instr_list
    { (fun mkTemp loc args -> 
                       mkStmt (Instr ($1 loc args)))
                  }
| ARG_s
    { let currentArg = $1 in
                    (fun mkTemp loc args -> 
                       match getArg currentArg args with
                         Fs s -> s
                       | a -> wrongArgType currentArg "stmt" a) }

stmt_list:
| 
    { (fun mkTemp loc args -> []) }
| ARG_S
    { let currentArg = $1 in
                   (fun mkTemp loc args -> 
                       match getArg currentArg args with 
                       | FS sl -> sl 
                       | a -> wrongArgType currentArg "stmts" a)
                 }
| stmt stmt_list
    { (fun mkTemp loc args -> 
                      let this = $1 mkTemp loc args in
                      this :: ($2 mkTemp loc args))
                 }
| type_spec attributes decl maybe_init SEMICOLON stmt_list
    { (fun mkTemp loc args -> 
                     let tal = (fst $2) args in
                     let ts  = (fst $1) tal args in
                     let (n, t, _) = (fst $3) ts args in
                     let init = $4 args in
                     (* Before we proceed we must create the variable *)
                     let v = mkTemp n t in
                     (* Now we parse the rest *)
                     let rest = $6 mkTemp loc ((n, Fv v) :: args) in
                     (* Now we add the initialization instruction to the 
                      * front *)
                     match init with 
                       NoInit -> rest
                     | InitExp e -> 
                         mkStmtOneInstr (Set((Var v, NoOffset), e, loc)) 
                         :: rest
                     | InitCall (f, args) ->
                         mkStmtOneInstr (Call(Some (Var v, NoOffset), 
                                              Lval f, args, loc))
                         :: rest

                                                           )
                 }

instr_list:
| instr %prec COMMA
    { (fun loc args -> [ ((fst $1) loc args) ]) }
| ARG_I
    { let currentArg = $1 in
                   (fun loc args -> 
                       match getArg currentArg args with 
                       | FI il -> il 
                       | a -> wrongArgType currentArg "instrs" a)
                 }
| instr instr_list
    { (fun loc args -> 
                      let this = (fst $1) loc args in
                      this :: ($2 loc args))
                 }

maybe_init:
| 
    { (fun args -> NoInit) }
| EQ expression
    { (fun args -> InitExp ((fst $2) args)) }
| EQ lval LPAREN arguments RPAREN
    { (fun args -> 
                                    InitCall((fst $2) args, (fst $4) args)) }

%%