modernc.org/cc@v1.0.1/model.go (about) 1 // Copyright 2016 The CC Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cc // import "modernc.org/cc" 6 7 import ( 8 "bytes" 9 "fmt" 10 "math" 11 "math/big" 12 "sort" 13 "strconv" 14 "strings" 15 16 "modernc.org/mathutil" 17 "modernc.org/xc" 18 ) 19 20 type ( 21 // StringLitID is the type of an Expression.Value representing the numeric 22 // ID of a string literal. 23 StringLitID int 24 25 // LongStringLitID is the type of an Expression.Value representing the 26 // numeric ID of a long string literal. 27 LongStringLitID int 28 29 // StringLitID is the type of an Expression.Value representing the numeric 30 // ID of a label name used in &&label. 31 ComputedGotoID int 32 ) 33 34 var ( 35 maxConvF32I32 = math.Nextafter32(math.MaxInt32, 0) // https://github.com/golang/go/issues/19405 36 maxConvF32U32 = math.Nextafter32(math.MaxUint32, 0) 37 ) 38 39 // ModelItem is a single item of a model. 40 // 41 // Note about StructAlign: To provide GCC ABI compatibility set, for example, 42 // Align of Double to 8 and StructAlign of Double to 4. 43 type ModelItem struct { 44 Size int // Size of the entity in bytes. 45 Align int // Alignment of the entity when it's not a struct field. 46 StructAlign int // Alignment of the entity when it's a struct field. 47 More interface{} // Optional user data. 48 } 49 50 // Model describes size and align requirements of predeclared types. 51 type Model struct { 52 Items map[Kind]ModelItem 53 54 BoolType Type 55 CharType Type 56 DoubleComplexType Type 57 DoubleType Type 58 FloatComplexType Type 59 FloatType Type 60 IntType Type 61 LongDoubleComplexType Type 62 LongDoubleType Type 63 LongLongType Type 64 LongType Type 65 ShortType Type 66 UCharType Type 67 UIntType Type 68 ULongLongType Type 69 ULongType Type 70 UShortType Type 71 UintPtrType Type 72 VoidType Type 73 longStrType Type 74 ptrDiffType Type 75 sizeType Type 76 strType Type 77 78 initialized bool 79 tweaks *tweaks 80 intConvRank [kindMax]int 81 Signed [kindMax]bool // Signed[Kind] reports whether Kind is a signed integer type. 82 promoteTo [kindMax]Kind 83 } 84 85 func (m *Model) initialize(lx *lexer) { 86 m.BoolType = m.makeType(lx, 0, tsBool) 87 m.CharType = m.makeType(lx, 0, tsChar) 88 m.DoubleComplexType = m.makeType(lx, 0, tsComplex, tsDouble) 89 m.DoubleType = m.makeType(lx, 0, tsDouble) 90 m.FloatComplexType = m.makeType(lx, 0, tsComplex, tsFloat) 91 m.FloatType = m.makeType(lx, 0, tsFloat) 92 m.IntType = m.makeType(lx, 0, tsInt) 93 m.LongDoubleComplexType = m.makeType(lx, 0, tsComplex, tsDouble, tsLong) 94 m.LongDoubleType = m.makeType(lx, 0, tsLong, tsDouble) 95 m.LongLongType = m.makeType(lx, 0, tsLong, tsLong) 96 m.LongType = m.makeType(lx, 0, tsLong) 97 m.ShortType = m.makeType(lx, 0, tsShort) 98 m.UCharType = m.makeType(lx, 0, tsUnsigned, tsChar) 99 m.UIntType = m.makeType(lx, 0, tsUnsigned) 100 m.ULongLongType = m.makeType(lx, 0, tsUnsigned, tsLong, tsLong) 101 m.ULongType = m.makeType(lx, 0, tsUnsigned, tsLong) 102 m.UShortType = m.makeType(lx, 0, tsUnsigned, tsShort) 103 m.UintPtrType = m.makeType(lx, 0, tsUintptr) // Pseudo type. 104 m.VoidType = m.makeType(lx, 0, tsVoid) 105 m.strType = m.makeType(lx, 0, tsChar).Pointer() 106 107 // [0], 6.3.1.1. 108 m.intConvRank = [kindMax]int{ 109 Bool: 1, 110 Char: 2, 111 SChar: 2, 112 UChar: 2, 113 Short: 3, 114 UShort: 3, 115 Int: 4, 116 UInt: 4, 117 Long: 5, 118 ULong: 5, 119 LongLong: 6, 120 ULongLong: 6, 121 UintPtr: 7, 122 } 123 m.Signed = [kindMax]bool{ 124 Char: true, 125 SChar: true, 126 Short: true, 127 Int: true, 128 Long: true, 129 LongLong: true, 130 } 131 m.promoteTo = [kindMax]Kind{} 132 for i := range m.promoteTo { 133 m.promoteTo[i] = Kind(i) 134 } 135 switch { 136 case m.tweaks.enableWideEnumValues: 137 m.intConvRank[Enum] = m.intConvRank[LongLong] 138 default: 139 m.intConvRank[Enum] = m.intConvRank[Int] 140 } 141 for k := Kind(0); k < kindMax; k++ { 142 r := m.intConvRank[k] 143 if r == 0 || r > m.intConvRank[Int] { 144 continue 145 } 146 147 // k is an integer type whose conversion rank is less than or 148 // equal to the rank of int and unsigned int. 149 switch { 150 case m.Items[k].Size < m.Items[Int].Size || m.Signed[k]: 151 // If an int can represent all values of the original 152 // type, the value is converted to an int; 153 m.promoteTo[k] = Int 154 default: 155 // otherwise, it is converted to an unsigned int. 156 m.promoteTo[k] = UInt 157 } 158 } 159 160 m.initialized = true 161 } 162 163 func (m *Model) typ(k Kind) Type { 164 switch k { 165 case Undefined: 166 return undefined 167 case Bool: 168 return m.BoolType 169 case Char: 170 return m.CharType 171 case Double: 172 return m.DoubleType 173 case Float: 174 return m.FloatType 175 case Int: 176 return m.IntType 177 case LongDouble: 178 return m.LongDoubleType 179 case LongLong: 180 return m.LongLongType 181 case Long: 182 return m.LongType 183 case Short: 184 return m.ShortType 185 case UChar: 186 return m.UCharType 187 case UInt: 188 return m.UIntType 189 case ULongLong: 190 return m.ULongLongType 191 case ULong: 192 return m.ULongType 193 case UShort: 194 return m.UShortType 195 case UintPtr: 196 return m.UintPtrType 197 case FloatComplex: 198 return m.FloatComplexType 199 case DoubleComplex: 200 return m.DoubleComplexType 201 case LongDoubleComplex: 202 return m.LongDoubleComplexType 203 case Enum: 204 switch { 205 case m.tweaks.enableWideEnumValues: 206 return m.LongLongType 207 default: 208 return m.IntType 209 } 210 default: 211 panic(k) 212 } 213 } 214 215 func (m *Model) enumValueToInt(v interface{}) (interface{}, bool) { 216 intSize := m.Items[Int].Size 217 if m.tweaks.enableWideEnumValues { 218 intSize = m.Items[LongLong].Size 219 } 220 switch x := v.(type) { 221 case byte, int8, int16, uint16, int32: 222 return m.MustConvert(x, m.IntType), true 223 case uint32: 224 switch intSize { 225 case 4: 226 return m.MustConvert(x, m.IntType), x <= math.MaxUint32 227 case 8: 228 return m.MustConvert(x, m.IntType), true 229 default: 230 panic(intSize) 231 } 232 case int64: 233 switch intSize { 234 case 4: 235 return m.MustConvert(x, m.IntType), x <= math.MaxUint32 236 case 8: 237 return m.MustConvert(x, m.IntType), true 238 default: 239 panic(intSize) 240 } 241 case uint64: 242 switch intSize { 243 case 4: 244 return m.MustConvert(x, m.IntType), x <= math.MaxUint32 245 case 8: 246 return m.MustConvert(x, m.IntType), x <= math.MaxUint64 247 default: 248 panic(intSize) 249 } 250 default: 251 panic(fmt.Errorf("%T", x)) 252 } 253 } 254 255 // sanityCheck reports model errors, if any. 256 func (m *Model) sanityCheck() error { 257 if len(m.Items) == 0 { 258 return fmt.Errorf("model has no items") 259 } 260 261 tab := map[Kind]struct { 262 minSize, maxSize int 263 minAlign, maxAlign int 264 }{ 265 Ptr: {4, 8, 4, 8}, 266 UintPtr: {4, 8, 4, 8}, 267 Void: {0, 0, 1, 1}, 268 Char: {1, 1, 1, 1}, 269 SChar: {1, 1, 1, 1}, 270 UChar: {1, 1, 1, 1}, 271 Short: {2, 2, 2, 2}, 272 UShort: {2, 2, 2, 2}, 273 Int: {4, 4, 4, 4}, 274 UInt: {4, 4, 4, 4}, 275 Long: {4, 8, 4, 8}, 276 ULong: {4, 8, 4, 8}, 277 LongLong: {8, 8, 8, 8}, 278 ULongLong: {8, 8, 8, 8}, 279 Float: {4, 4, 4, 4}, 280 Double: {8, 8, 8, 8}, 281 LongDouble: {8, 16, 8, 16}, 282 Bool: {1, 1, 1, 1}, 283 FloatComplex: {8, 8, 8, 8}, 284 DoubleComplex: {16, 16, 8, 16}, 285 LongDoubleComplex: {16, 32, 8, 16}, 286 } 287 a := []int{} 288 required := map[Kind]bool{} 289 seen := map[Kind]bool{} 290 for k := range tab { 291 required[k] = true 292 a = append(a, int(k)) 293 } 294 sort.Ints(a) 295 for k, v := range m.Items { 296 if seen[k] { 297 return fmt.Errorf("model has duplicate item: %s", k) 298 } 299 300 seen[k] = true 301 if !required[k] { 302 return fmt.Errorf("model has invalid type: %s: %#v", k, v) 303 } 304 305 for typ, t := range tab { 306 if typ == k { 307 if v.Size < t.minSize { 308 return fmt.Errorf("size %d too small: %s", v.Size, k) 309 } 310 311 if v.Size > t.maxSize { 312 return fmt.Errorf("size %d too big: %s", v.Size, k) 313 } 314 315 if v.Size != 0 && mathutil.PopCount(v.Size) != 1 { 316 return fmt.Errorf("size %d is not a power of two: %s", v.Size, k) 317 } 318 319 if v.Align < t.minAlign { 320 return fmt.Errorf("align %d too small: %s", v.Align, k) 321 } 322 323 if v.Align > t.maxAlign { 324 return fmt.Errorf("align %d too big: %s", v.Align, k) 325 } 326 327 if v.Align < v.Size && v.Align < t.minAlign { 328 return fmt.Errorf("align is smaller than size: %s", k) 329 } 330 331 if v.StructAlign < 1 { 332 return fmt.Errorf("struct align %d too small: %s", v.StructAlign, k) 333 } 334 335 if v.StructAlign > t.maxAlign { 336 return fmt.Errorf("struct align %d too big: %s", v.Align, k) 337 } 338 339 if mathutil.PopCount(v.Align) != 1 { 340 return fmt.Errorf("align %d is not a power of two: %s", v.Align, k) 341 } 342 343 break 344 } 345 } 346 } 347 w := m.Items[Ptr].Size 348 if m.Items[Short].Size < w && 349 m.Items[Int].Size < w && 350 m.Items[Long].Size < w && 351 m.Items[LongLong].Size < w { 352 return fmt.Errorf("model has no integer type suitable for pointer difference and sizeof") 353 } 354 355 for _, typ := range a { 356 if !seen[Kind(typ)] { 357 return fmt.Errorf("model has no item for type %s", Kind(typ)) 358 } 359 } 360 361 if g, e := w, m.Items[UintPtr].Size; g != e { 362 return fmt.Errorf("model uintptr has different sizes than ptr has") 363 } 364 return nil 365 } 366 367 // MustConvert returns v converted to the type of typ. If the conversion is 368 // impossible, the method panics. 369 // 370 // Conversion an integer type to any pointer type yields an uintptr. 371 func (m *Model) MustConvert(v interface{}, typ Type) interface{} { 372 if typ.Kind() == Enum { 373 typ = m.IntType 374 } 375 mi, ok := m.Items[typ.Kind()] 376 if !ok && typ.Kind() != Function { 377 panic(fmt.Errorf("internal error: no model item for %s, %s", typ, typ.Kind())) 378 } 379 380 w := mi.Size 381 switch typ.Kind() { 382 case Short: 383 switch x := v.(type) { 384 case int32: 385 switch w { 386 case 2: 387 return int16(x) 388 default: 389 panic(w) 390 } 391 case int64: 392 switch w { 393 case 2: 394 return int16(x) 395 default: 396 panic(w) 397 } 398 default: 399 panic(fmt.Errorf("internal error %T", x)) 400 } 401 case UShort: 402 switch x := v.(type) { 403 case uint16: 404 switch w { 405 case 2: 406 return x 407 default: 408 panic(w) 409 } 410 case int32: 411 switch w { 412 case 2: 413 return uint16(x) 414 default: 415 panic(w) 416 } 417 case uint32: 418 switch w { 419 case 2: 420 return uint16(x) 421 default: 422 panic(w) 423 } 424 default: 425 panic(fmt.Errorf("internal error %T", x)) 426 } 427 case Int: 428 switch x := v.(type) { 429 case int8: 430 switch w { 431 case 4: 432 return int32(x) 433 default: 434 panic(w) 435 } 436 case byte: 437 switch w { 438 case 4: 439 return int32(x) 440 default: 441 panic(w) 442 } 443 case int16: 444 switch w { 445 case 4: 446 return int32(x) 447 default: 448 panic(w) 449 } 450 case uint16: 451 switch w { 452 case 4: 453 return int32(x) 454 default: 455 panic(w) 456 } 457 case int32: 458 switch w { 459 case 4: 460 return x 461 default: 462 panic(w) 463 } 464 case uint32: 465 switch w { 466 case 4: 467 return int32(x) 468 default: 469 panic(w) 470 } 471 case int64: 472 switch w { 473 case 4: 474 return int32(x) 475 default: 476 panic(w) 477 } 478 case uint64: 479 switch w { 480 case 4: 481 return int32(x) 482 default: 483 panic(w) 484 } 485 case float32: 486 switch w { 487 case 4: 488 switch { 489 case x > maxConvF32I32: 490 return int32(math.MaxInt32) 491 default: 492 return int32(x) 493 } 494 default: 495 panic(w) 496 } 497 case float64: 498 switch w { 499 case 4: 500 return int32(x) 501 default: 502 panic(w) 503 } 504 default: 505 panic(fmt.Errorf("internal error %T", x)) 506 } 507 case UInt: 508 switch x := v.(type) { 509 case uint8: 510 switch w { 511 case 4: 512 return uint32(x) 513 default: 514 panic(w) 515 } 516 case int16: 517 switch w { 518 case 4: 519 return uint32(x) 520 default: 521 panic(w) 522 } 523 case uint16: 524 switch w { 525 case 4: 526 return uint32(x) 527 default: 528 panic(w) 529 } 530 case int32: 531 switch w { 532 case 4: 533 return uint32(x) 534 default: 535 panic(w) 536 } 537 case uint32: 538 switch w { 539 case 4: 540 return x 541 default: 542 panic(w) 543 } 544 case int64: 545 switch w { 546 case 4: 547 return uint32(x) 548 default: 549 panic(w) 550 } 551 case uint64: 552 switch w { 553 case 4: 554 return uint32(x) 555 default: 556 panic(w) 557 } 558 case uintptr: 559 switch w { 560 case 4: 561 return uint32(x) 562 default: 563 panic(w) 564 } 565 case float32: 566 switch w { 567 case 4: 568 switch { 569 case x > maxConvF32U32: 570 return uint32(math.MaxUint32) 571 default: 572 return uint32(x) 573 } 574 default: 575 panic(w) 576 } 577 case float64: 578 switch w { 579 case 4: 580 return uint32(x) 581 default: 582 panic(w) 583 } 584 default: 585 panic(fmt.Errorf("internal error %T", x)) 586 } 587 case Long: 588 switch x := v.(type) { 589 case int16: 590 switch w { 591 case 4: 592 return int32(x) 593 case 8: 594 return int64(x) 595 default: 596 panic(w) 597 } 598 case int32: 599 switch w { 600 case 4: 601 return x 602 case 8: 603 return int64(x) 604 default: 605 panic(w) 606 } 607 case uint32: 608 switch w { 609 case 4: 610 return int32(x) 611 case 8: 612 return int64(x) 613 default: 614 panic(w) 615 } 616 case int64: 617 switch w { 618 case 4: 619 return int32(x) 620 case 8: 621 return x 622 default: 623 panic(w) 624 } 625 case uint64: 626 switch w { 627 case 4: 628 return int32(x) 629 case 8: 630 return int64(x) 631 default: 632 panic(w) 633 } 634 case uintptr: 635 switch w { 636 case 4: 637 return int32(x) 638 case 8: 639 return int64(x) 640 default: 641 panic(w) 642 } 643 default: 644 panic(fmt.Errorf("internal error %T", x)) 645 } 646 case LongLong: 647 switch x := v.(type) { 648 case int32: 649 switch w { 650 case 8: 651 return int64(x) 652 default: 653 panic(w) 654 } 655 case uint32: 656 switch w { 657 case 8: 658 return int64(x) 659 default: 660 panic(w) 661 } 662 case int64: 663 switch w { 664 case 8: 665 return x 666 default: 667 panic(w) 668 } 669 case uint64: 670 switch w { 671 case 8: 672 return int64(x) 673 default: 674 panic(w) 675 } 676 default: 677 panic(fmt.Errorf("internal error %T", x)) 678 } 679 case ULong: 680 switch x := v.(type) { 681 case uint8: 682 switch w { 683 case 4: 684 return uint32(x) 685 case 8: 686 return uint64(x) 687 default: 688 panic(w) 689 } 690 case int: 691 switch w { 692 case 4: 693 return uint32(x) 694 case 8: 695 return uint64(x) 696 default: 697 panic(w) 698 } 699 case int32: 700 switch w { 701 case 4: 702 return uint32(x) 703 case 8: 704 return uint64(x) 705 default: 706 panic(w) 707 } 708 case int64: 709 switch w { 710 case 4: 711 return uint32(x) 712 case 8: 713 return uint64(x) 714 default: 715 panic(w) 716 } 717 case uint32: 718 switch w { 719 case 4: 720 return x 721 case 8: 722 return uint64(x) 723 default: 724 panic(w) 725 } 726 case uint64: 727 switch w { 728 case 4: 729 return uint32(x) 730 case 8: 731 return x 732 default: 733 panic(w) 734 } 735 case uintptr: 736 switch w { 737 case 4: 738 return uint32(x) 739 case 8: 740 return uint64(x) 741 default: 742 panic(w) 743 } 744 default: 745 panic(fmt.Errorf("internal error %T", x)) 746 } 747 case ULongLong: 748 switch x := v.(type) { 749 case int32: 750 switch w { 751 case 8: 752 return uint64(x) 753 default: 754 panic(w) 755 } 756 case int64: 757 switch w { 758 case 8: 759 return uint64(x) 760 default: 761 panic(w) 762 } 763 case uint32: 764 switch w { 765 case 8: 766 return uint64(x) 767 default: 768 panic(w) 769 } 770 case uint64: 771 switch w { 772 case 8: 773 return x 774 default: 775 panic(w) 776 } 777 case uintptr: 778 switch w { 779 case 8: 780 return uint64(x) 781 default: 782 panic(w) 783 } 784 default: 785 panic(fmt.Errorf("internal error %T", x)) 786 } 787 case Float: 788 switch x := v.(type) { 789 case int32: 790 switch w { 791 case 4: 792 return float32(x) 793 case 8: 794 return float64(x) 795 default: 796 panic(w) 797 } 798 case uint32: 799 switch w { 800 case 4: 801 return float32(x) 802 case 8: 803 return float64(x) 804 default: 805 panic(w) 806 } 807 case int64: 808 switch w { 809 case 4: 810 return float32(x) 811 case 8: 812 return float64(x) 813 default: 814 panic(w) 815 } 816 case uint64: 817 switch w { 818 case 4: 819 return float32(x) 820 case 8: 821 return float64(x) 822 default: 823 panic(w) 824 } 825 case float32: 826 switch w { 827 case 4: 828 return x 829 case 8: 830 return float64(x) 831 default: 832 panic(w) 833 } 834 case float64: 835 switch w { 836 case 4: 837 return float32(x) 838 case 8: 839 return x 840 default: 841 panic(w) 842 } 843 default: 844 panic(fmt.Errorf("internal error %T", x)) 845 } 846 case Double: 847 switch x := v.(type) { 848 case int32: 849 switch w { 850 case 8: 851 return float64(x) 852 default: 853 panic(w) 854 } 855 case uint32: 856 switch w { 857 case 8: 858 return float64(x) 859 default: 860 panic(w) 861 } 862 case int64: 863 switch w { 864 case 8: 865 return float64(x) 866 default: 867 panic(w) 868 } 869 case uint64: 870 switch w { 871 case 8: 872 return float64(x) 873 default: 874 panic(w) 875 } 876 case float32: 877 switch w { 878 case 8: 879 return float64(x) 880 default: 881 panic(w) 882 } 883 case float64: 884 switch w { 885 case 8: 886 return x 887 default: 888 panic(w) 889 } 890 default: 891 panic(fmt.Errorf("internal error %T", x)) 892 } 893 case Ptr, Function: 894 switch x := v.(type) { 895 case int32: 896 return uintptr(x) 897 case uint32: 898 return uintptr(x) 899 case int64: 900 return uintptr(x) 901 case uint64: 902 return uintptr(x) 903 case uintptr: 904 return x 905 case StringLitID: 906 return nil 907 default: 908 panic(fmt.Errorf("internal error %T", x)) 909 } 910 case Void: 911 return nil 912 case Char, SChar: 913 switch x := v.(type) { 914 case int32: 915 switch w { 916 case 1: 917 return int8(x) 918 default: 919 panic(w) 920 } 921 case uint32: 922 switch w { 923 case 1: 924 return int8(x) 925 default: 926 panic(w) 927 } 928 default: 929 panic(fmt.Errorf("internal error %T", x)) 930 } 931 case UChar: 932 switch x := v.(type) { 933 case uint8: 934 switch w { 935 case 1: 936 return x 937 default: 938 panic(w) 939 } 940 case int32: 941 switch w { 942 case 1: 943 return byte(x) 944 default: 945 panic(w) 946 } 947 case uint32: 948 switch w { 949 case 1: 950 return byte(x) 951 default: 952 panic(w) 953 } 954 default: 955 panic(fmt.Errorf("internal error %T", x)) 956 } 957 case UintPtr: 958 switch x := v.(type) { 959 case int32: 960 switch w { 961 case 4, 8: 962 return uintptr(x) 963 default: 964 panic(w) 965 } 966 case uint32: 967 switch w { 968 case 4: 969 return uintptr(x) 970 default: 971 panic(w) 972 } 973 case uint64: 974 switch w { 975 case 8: 976 return uintptr(x) 977 default: 978 panic(w) 979 } 980 default: 981 panic(fmt.Errorf("internal error %T", x)) 982 } 983 case LongDouble: 984 switch x := v.(type) { 985 case int32: 986 switch w { 987 case 8, 16: 988 return float64(x) 989 default: 990 panic(w) 991 } 992 case uint32: 993 switch w { 994 case 8, 16: 995 return float64(x) 996 default: 997 panic(w) 998 } 999 case int64: 1000 switch w { 1001 case 8, 16: 1002 return float64(x) 1003 default: 1004 panic(w) 1005 } 1006 case uint64: 1007 switch w { 1008 case 8, 16: 1009 return float64(x) 1010 default: 1011 panic(w) 1012 } 1013 case float32: 1014 switch w { 1015 case 8, 16: 1016 return float64(x) 1017 default: 1018 panic(w) 1019 } 1020 case float64: 1021 switch w { 1022 case 8, 16: 1023 return x 1024 default: 1025 panic(w) 1026 } 1027 default: 1028 panic(fmt.Errorf("internal error %T", x)) 1029 } 1030 case Bool: 1031 switch x := v.(type) { 1032 case int32: 1033 if x != 0 { 1034 return int32(1) 1035 } 1036 1037 return int32(0) 1038 default: 1039 panic(fmt.Errorf("internal error %T", x)) 1040 } 1041 case FloatComplex: 1042 switch x := v.(type) { 1043 case float32: 1044 switch w { 1045 case 8: 1046 return complex(x, 0) 1047 default: 1048 panic(w) 1049 } 1050 case float64: 1051 switch w { 1052 case 8: 1053 return complex(float32(x), 0) 1054 default: 1055 panic(w) 1056 } 1057 case complex64: 1058 switch w { 1059 case 8: 1060 return x 1061 default: 1062 panic(w) 1063 } 1064 case complex128: 1065 switch w { 1066 case 8: 1067 return complex64(x) 1068 default: 1069 panic(w) 1070 } 1071 default: 1072 panic(fmt.Errorf("internal error %T", x)) 1073 } 1074 case DoubleComplex: 1075 switch x := v.(type) { 1076 case int32: 1077 switch w { 1078 case 16: 1079 return complex(float64(x), 0) 1080 default: 1081 panic(w) 1082 } 1083 case float64: 1084 switch w { 1085 case 16: 1086 return complex(x, 0) 1087 default: 1088 panic(w) 1089 } 1090 case complex128: 1091 switch w { 1092 case 16: 1093 return x 1094 default: 1095 panic(w) 1096 } 1097 default: 1098 panic(fmt.Errorf("internal error %T", x)) 1099 } 1100 case LongDoubleComplex: 1101 switch x := v.(type) { 1102 case float64: 1103 switch w { 1104 case 16: 1105 return complex(x, 0) 1106 default: 1107 panic(w) 1108 } 1109 case complex128: 1110 switch w { 1111 case 16: 1112 return x 1113 default: 1114 panic(w) 1115 } 1116 default: 1117 panic(fmt.Errorf("internal error %T", x)) 1118 } 1119 default: 1120 panic(fmt.Errorf("internal error %s, %s", typ, typ.Kind())) 1121 } 1122 } 1123 1124 func (m *Model) value2(v interface{}, typ Type) (interface{}, Type) { 1125 return m.MustConvert(v, typ), typ 1126 } 1127 1128 func (m *Model) charConst(lx *lexer, t xc.Token) (interface{}, Type) { 1129 switch t.Rune { 1130 case CHARCONST: 1131 s := string(t.S()) 1132 typ := m.IntType 1133 var r rune 1134 s = s[1 : len(s)-1] // Remove outer 's. 1135 if len(s) == 1 { 1136 return rune(s[0]), m.IntType 1137 } 1138 1139 runes := []rune(s) 1140 switch runes[0] { 1141 case '\\': 1142 r, _ = decodeEscapeSequence(runes) 1143 if r < 0 { 1144 r = -r 1145 } 1146 default: 1147 r = runes[0] 1148 } 1149 return r, typ 1150 case LONGCHARCONST: 1151 s := t.S() 1152 typ := m.LongType 1153 var buf bytes.Buffer 1154 s = s[2 : len(s)-1] 1155 runes := []rune(string(s)) 1156 for i := 0; i < len(runes); { 1157 switch r := runes[i]; { 1158 case r == '\\': 1159 r, n := decodeEscapeSequence(runes[i:]) 1160 switch { 1161 case r < 0: 1162 buf.WriteByte(byte(-r)) 1163 default: 1164 buf.WriteRune(r) 1165 } 1166 i += n 1167 default: 1168 buf.WriteByte(byte(r)) 1169 i++ 1170 } 1171 } 1172 s = buf.Bytes() 1173 runes = []rune(string(s)) 1174 if len(runes) != 1 { 1175 lx.report.Err(t.Pos(), "invalid character literal %s", t.S()) 1176 return 0, typ 1177 } 1178 1179 return runes[0], typ 1180 default: 1181 panic("internal error") 1182 } 1183 } 1184 1185 func (m *Model) getSizeType(lx *lexer) Type { 1186 if t := m.sizeType; t != nil { 1187 return t 1188 } 1189 1190 b := lx.scope.Lookup(NSIdentifiers, xc.Dict.SID("size_t")) 1191 if b.Node == nil { 1192 w := m.Items[Ptr].Size 1193 switch { 1194 case m.Items[Short].Size >= w: 1195 return m.ShortType 1196 case m.Items[Int].Size >= w: 1197 return m.IntType 1198 case m.Items[Long].Size >= w: 1199 return m.LongType 1200 default: 1201 return m.LongLongType 1202 } 1203 } 1204 1205 d := b.Node.(*DirectDeclarator) 1206 if !d.TopDeclarator().RawSpecifier().IsTypedef() { 1207 lx.report.Err(d.Pos(), "size_t is not a typedef name") 1208 m.sizeType = undefined 1209 return undefined 1210 } 1211 1212 m.sizeType = b.Node.(*DirectDeclarator).top().declarator.Type 1213 return m.sizeType 1214 } 1215 1216 func (m *Model) getPtrDiffType(lx *lexer) Type { 1217 if t := m.ptrDiffType; t != nil { 1218 return t 1219 } 1220 1221 b := lx.scope.Lookup(NSIdentifiers, xc.Dict.SID("ptrdiff_t")) 1222 if b.Node == nil { 1223 w := m.Items[Ptr].Size 1224 switch { 1225 case m.Items[Short].Size >= w: 1226 return m.ShortType 1227 case m.Items[Int].Size >= w: 1228 return m.IntType 1229 case m.Items[Long].Size >= w: 1230 return m.LongType 1231 default: 1232 return m.LongLongType 1233 } 1234 } 1235 1236 d := b.Node.(*DirectDeclarator) 1237 if !d.TopDeclarator().RawSpecifier().IsTypedef() { 1238 lx.report.Err(d.Pos(), "ptrdiff_t is not a typedef name") 1239 m.ptrDiffType = undefined 1240 return undefined 1241 } 1242 1243 m.ptrDiffType = b.Node.(*DirectDeclarator).top().declarator.Type 1244 return m.ptrDiffType 1245 } 1246 1247 func (m *Model) getLongStrType(lx *lexer, tok xc.Token) Type { 1248 if t := m.longStrType; t != nil { 1249 return t 1250 } 1251 1252 b := lx.scope.Lookup(NSIdentifiers, xc.Dict.SID("wchar_t")) 1253 if b.Node == nil { 1254 m.longStrType = m.IntType.Pointer() 1255 return m.longStrType 1256 } 1257 1258 d := b.Node.(*DirectDeclarator) 1259 if !d.TopDeclarator().RawSpecifier().IsTypedef() { 1260 lx.report.Err(d.Pos(), "wchar_t is not a typedef name") 1261 m.longStrType = undefined 1262 return m.longStrType 1263 } 1264 1265 m.longStrType = b.Node.(*DirectDeclarator).top().declarator.Type.Pointer() 1266 return m.longStrType 1267 } 1268 1269 func (m *Model) strConst(lx *lexer, t xc.Token) (interface{}, Type) { 1270 s := t.S() 1271 typ := m.strType 1272 var buf bytes.Buffer 1273 switch t.Rune { 1274 case LONGSTRINGLITERAL: 1275 typ = m.getLongStrType(lx, t) 1276 s = s[1:] // Remove leading 'L'. 1277 fallthrough 1278 case STRINGLITERAL: 1279 s = s[1 : len(s)-1] // Remove outer "s. 1280 runes := []rune(string(s)) 1281 for i := 0; i < len(runes); { 1282 switch r := runes[i]; { 1283 case r == '\\': 1284 r, n := decodeEscapeSequence(runes[i:]) 1285 switch { 1286 case r < 0: 1287 buf.WriteByte(byte(-r)) 1288 default: 1289 buf.WriteRune(r) 1290 } 1291 i += n 1292 default: 1293 buf.WriteByte(byte(r)) 1294 i++ 1295 } 1296 } 1297 default: 1298 panic("internal error") 1299 } 1300 s = buf.Bytes() 1301 switch t.Rune { 1302 case LONGSTRINGLITERAL: 1303 return LongStringLitID(xc.Dict.ID(s)), typ 1304 case STRINGLITERAL: 1305 return StringLitID(xc.Dict.ID(s)), typ 1306 default: 1307 panic("internal error") 1308 } 1309 } 1310 1311 func (m *Model) floatConst(lx *lexer, t xc.Token) (interface{}, Type) { 1312 const ( 1313 f = 1 << iota 1314 j 1315 l 1316 ) 1317 k := 0 1318 s := t.S() 1319 i := len(s) - 1 1320 more: 1321 switch c := s[i]; c { 1322 case 'i', 'j': 1323 k |= j 1324 i-- 1325 goto more 1326 case 'l', 'L': 1327 k |= l 1328 i-- 1329 goto more 1330 case 'f', 'F': 1331 k |= f 1332 i-- 1333 goto more 1334 } 1335 if k&j != 0 && !lx.tweaks.enableImaginarySuffix { 1336 lx.report.Err(t.Pos(), "imaginary suffixes not enabled") 1337 k &^= j 1338 } 1339 ss := string(s[:i+1]) 1340 var v float64 1341 var err error 1342 switch { 1343 case strings.Contains(ss, "p"): 1344 var bf *big.Float 1345 bf, _, err = big.ParseFloat(ss, 0, 53, big.ToNearestEven) 1346 switch { 1347 case err != nil: 1348 lx.report.Err(t.Pos(), "invalid floating point constant %s", ss) 1349 v = 0 1350 default: 1351 v, _ = bf.Float64() 1352 } 1353 default: 1354 v, err = strconv.ParseFloat(ss, 64) 1355 } 1356 if err != nil { 1357 lx.report.Err(t.Pos(), "invalid floating point constant %s", ss) 1358 v = 0 1359 } 1360 switch k { 1361 case 0: 1362 return m.value2(v, m.DoubleType) 1363 case l: 1364 return m.value2(v, m.LongDoubleType) 1365 case j: 1366 return m.value2(complex(0, v), m.DoubleComplexType) 1367 case j | l: 1368 return m.value2(complex(0, v), m.LongDoubleComplexType) 1369 case f: 1370 return m.value2(v, m.FloatType) 1371 case f | j: 1372 return m.value2(complex(0, v), m.FloatComplexType) 1373 default: 1374 lx.report.Err(t.Pos(), "invalid literal %s", t.S()) 1375 return 0.0, m.DoubleType 1376 } 1377 } 1378 1379 func (m *Model) intConst(lx *lexer, t xc.Token) (interface{}, Type) { 1380 const ( 1381 l = 1 << iota 1382 ll 1383 u 1384 ) 1385 k := 0 1386 s := t.S() 1387 i := len(s) - 1 1388 more: 1389 switch c := s[i]; c { 1390 case 'u', 'U': 1391 k |= u 1392 i-- 1393 goto more 1394 case 'l', 'L': 1395 if i > 0 && (s[i-1] == 'l' || s[i-1] == 'L') { 1396 k |= ll 1397 i -= 2 1398 goto more 1399 } 1400 1401 k |= l 1402 i-- 1403 goto more 1404 } 1405 n, err := strconv.ParseUint(string(s[:i+1]), 0, 64) 1406 if err != nil { 1407 lx.report.Err(t.Pos(), "invalid integer constant: %s", s) 1408 } 1409 1410 switch k { 1411 case 0: 1412 switch b := mathutil.BitLenUint64(n); { 1413 case b < 32: 1414 return m.value2(n, m.IntType) 1415 case b < 33: 1416 return m.value2(n, m.UIntType) 1417 case b < 64: 1418 if m.Items[Long].Size == 8 { 1419 return m.value2(n, m.LongType) 1420 } 1421 1422 return m.value2(n, m.LongLongType) 1423 default: 1424 if m.Items[ULong].Size == 8 { 1425 return m.value2(n, m.ULongType) 1426 } 1427 1428 return m.value2(n, m.ULongLongType) 1429 } 1430 case l: 1431 return m.value2(n, m.LongType) 1432 case ll: 1433 return m.value2(n, m.LongLongType) 1434 case u: 1435 return m.value2(n, m.UIntType) 1436 case u | l: 1437 return m.value2(n, m.ULongType) 1438 case u | ll: 1439 return m.value2(n, m.ULongLongType) 1440 default: 1441 panic("internal error") 1442 } 1443 } 1444 1445 func (m *Model) cBool(v bool) interface{} { 1446 if v { 1447 return m.MustConvert(int32(1), m.IntType) 1448 1449 } 1450 return m.MustConvert(int32(0), m.IntType) 1451 } 1452 1453 func (m *Model) binOp(lx *lexer, a, b operand) (interface{}, interface{}, Type) { 1454 av, at := a.eval(lx) 1455 bv, bt := b.eval(lx) 1456 t := at 1457 if IsArithmeticType(at) && IsArithmeticType(bt) { 1458 t = m.BinOpType(at, bt) 1459 } 1460 if av == nil || bv == nil || t.Kind() == Undefined { 1461 return nil, nil, t 1462 } 1463 1464 return m.MustConvert(av, t), m.MustConvert(bv, t), t 1465 } 1466 1467 // BinOpType returns the evaluation type of a binop b, ie. the type operands 1468 // are converted to before performing the operation. Operands must be 1469 // arithmetic types. 1470 // 1471 // See [0], 6.3.1.8 - Usual arithmetic conversions. 1472 func (m *Model) BinOpType(a, b Type) Type { 1473 ak := a.Kind() 1474 bk := b.Kind() 1475 1476 if ak == LongDoubleComplex || bk == LongDoubleComplex { 1477 return m.LongDoubleComplexType 1478 } 1479 1480 if ak == DoubleComplex || bk == DoubleComplex { 1481 return m.DoubleComplexType 1482 } 1483 1484 if ak == FloatComplex || bk == FloatComplex { 1485 return m.FloatComplexType 1486 } 1487 1488 // First, if the corresponding real type of either operand is long 1489 // double, the other operand is converted, without change of type 1490 // domain, to a type whose corresponding real type is long double. 1491 if ak == LongDouble || bk == LongDouble { 1492 return m.LongDoubleType 1493 } 1494 1495 // Otherwise, if the corresponding real type of either operand is 1496 // double, the other operand is converted, without change of type 1497 // domain, to a type whose corresponding real type is double. 1498 if ak == Double || bk == Double { 1499 return m.DoubleType 1500 } 1501 1502 // Otherwise, if the corresponding real type of either operand is float, the other 1503 // operand is converted, without change of type domain, to a type whose 1504 // corresponding real type is float. 1505 if ak == Float || bk == Float { 1506 return m.FloatType 1507 } 1508 1509 // Otherwise, the integer promotions are performed on both operands. 1510 ak = m.promoteTo[ak] 1511 bk = m.promoteTo[bk] 1512 1513 // Then the following rules are applied to the promoted operands: 1514 ar := m.intConvRank[ak] 1515 br := m.intConvRank[bk] 1516 1517 // If both operands have the same type, then no further conversion is 1518 // needed. 1519 if ak == bk { 1520 return m.typ(ak) 1521 } 1522 1523 // Otherwise, if both operands have signed integer types or both have 1524 // unsigned integer types, the operand with the type of lesser integer 1525 // conversion rank is converted to the type of the operand with greater 1526 // rank. 1527 if m.Signed[ak] == m.Signed[bk] { 1528 switch { 1529 case ar < br: 1530 return m.typ(bk) 1531 default: 1532 return m.typ(ak) 1533 } 1534 } 1535 1536 // Make a the unsigned type and b the signed type. 1537 if m.Signed[ak] { 1538 a, b = b, a 1539 ak, bk = bk, ak 1540 ar, br = br, ar 1541 } 1542 1543 // Otherwise, if the operand that has unsigned integer type has rank 1544 // greater or equal to the rank of the type of the other operand, then 1545 // the operand with signed integer type is converted to the type of the 1546 // operand with unsigned integer type. 1547 if ar >= br { 1548 return m.typ(ak) 1549 } 1550 1551 // Otherwise, if the type of the operand with signed integer type can 1552 // represent all of the values of the type of the operand with unsigned 1553 // integer type, then the operand with unsigned integer type is 1554 // converted to the type of the operand with signed integer type. 1555 as := m.Items[ak].Size 1556 bs := m.Items[bk].Size 1557 if bs > as { 1558 return m.typ(bk) 1559 } 1560 1561 // Otherwise, both operands are converted to the unsigned integer type 1562 // corresponding to the type of the operand with signed integer type. 1563 return m.typ(unsigned(bk)) 1564 } 1565 1566 func (m *Model) promote(t Type) Type { 1567 if !IsIntType(t) { 1568 return t 1569 } 1570 1571 return m.BinOpType(t, t) 1572 } 1573 1574 func (m *Model) makeType(lx *lexer, attr int, ts ...int) Type { 1575 d := m.makeDeclarator(attr, ts...) 1576 return d.setFull(lx) 1577 } 1578 1579 func (m *Model) makeDeclarator(attr int, ts ...int) *Declarator { 1580 s := &spec{attr, tsEncode(ts...)} 1581 d := &Declarator{specifier: s} 1582 dd := &DirectDeclarator{declarator: d, specifier: s} 1583 d.DirectDeclarator = dd 1584 return d 1585 } 1586 1587 func (m *Model) checkArithmeticType(lx *lexer, a ...operand) (r bool) { 1588 r = true 1589 for _, v := range a { 1590 _, t := v.eval(lx) 1591 if !IsArithmeticType(t) { 1592 lx.report.Err(v.Pos(), "not an arithmetic type (have '%s')", t) 1593 r = false 1594 } 1595 } 1596 return r 1597 } 1598 1599 func (m *Model) checkIntegerOrBoolType(lx *lexer, a ...operand) (r bool) { 1600 r = true 1601 for _, v := range a { 1602 _, t := v.eval(lx) 1603 if !IsIntType(t) && !(t.Kind() == Bool) { 1604 lx.report.Err(v.Pos(), "not an integer or bool type (have '%s')", t) 1605 r = false 1606 } 1607 } 1608 return r 1609 }