|
| 1 | +(* ============================================================================= |
| 2 | + CodeHawk C Analyzer |
| 3 | + Author: Henny Sipma |
| 4 | + ------------------------------------------------------------------------------ |
| 5 | + The MIT License (MIT) |
| 6 | +
|
| 7 | + Copyright (c) 2005-2019 Kestrel Technology LLC |
| 8 | + Copyright (c) 2020-2024 Henny B. Sipma |
| 9 | + Copyright (c) 2024-2026 Aarno Labs LLC |
| 10 | +
|
| 11 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | + of this software and associated documentation files (the "Software"), to deal |
| 13 | + in the Software without restriction, including without limitation the rights |
| 14 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | + copies of the Software, and to permit persons to whom the Software is |
| 16 | + furnished to do so, subject to the following conditions: |
| 17 | +
|
| 18 | + The above copyright notice and this permission notice shall be included in all |
| 19 | + copies or substantial portions of the Software. |
| 20 | +
|
| 21 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 27 | + SOFTWARE. |
| 28 | + ============================================================================= *) |
| 29 | + |
| 30 | +(* chutil *) |
| 31 | +open CHLogger |
| 32 | +open CHTraceResult |
| 33 | + |
| 34 | +(* cchlib *) |
| 35 | +open CCHBasicTypes |
| 36 | +open CCHTypesToPretty |
| 37 | +open CCHUtilities |
| 38 | + |
| 39 | + |
| 40 | +let p2s = CHPrettyUtil.pretty_to_string |
| 41 | + |
| 42 | +let eloc (line: int): string = __FILE__ ^ ":" ^ (string_of_int line) |
| 43 | +let elocm (line: int): string = (eloc line) ^ ": " |
| 44 | + |
| 45 | + |
| 46 | +let get_integer_promotion (ty: typ): typ = |
| 47 | + let promote_ikind (ik: ikind): ikind = |
| 48 | + match ik with |
| 49 | + | IChar | ISChar | IUChar | IShort | IBool -> IInt |
| 50 | + | IUShort -> IUInt |
| 51 | + | _ -> ik in |
| 52 | + |
| 53 | + match ty with |
| 54 | + | TInt (ik, _) -> TInt (promote_ikind ik, []) |
| 55 | + | TEnum (ename, _) -> |
| 56 | + let einfo = CCHFileEnvironment.file_environment#get_enum ename in |
| 57 | + TInt (promote_ikind einfo.ekind, []) |
| 58 | + | _ -> |
| 59 | + let _ = |
| 60 | + log_diagnostics_result |
| 61 | + ~tag:"get_integer_promotion: unexpected type" |
| 62 | + __FILE__ __LINE__ |
| 63 | + [p2s (typ_to_pretty ty)] in |
| 64 | + ty |
| 65 | + |
| 66 | + |
| 67 | +let usual_arithmetic_conversion (ty1: typ) (ty2: typ): typ traceresult = |
| 68 | + |
| 69 | + let convert_float (fk1: fkind) (fk2: fkind): fkind = |
| 70 | + let is_complex (fk: fkind) = |
| 71 | + match fk with |
| 72 | + | FComplexLongDouble | FComplexDouble | FComplexFloat -> true |
| 73 | + | _ -> false in |
| 74 | + match fk1, fk2 with |
| 75 | + | FComplexLongDouble, _ -> FComplexLongDouble |
| 76 | + | _, FComplexLongDouble -> FComplexLongDouble |
| 77 | + | FComplexDouble, _ -> FComplexDouble |
| 78 | + | _, FComplexDouble -> FComplexDouble |
| 79 | + | FComplexFloat, _ -> FComplexFloat |
| 80 | + | _, FComplexFloat -> FComplexFloat |
| 81 | + | FLongDouble, other when is_complex other -> FComplexLongDouble |
| 82 | + | other, FLongDouble when is_complex other -> FComplexLongDouble |
| 83 | + | FLongDouble, _ | _, FLongDouble -> FLongDouble |
| 84 | + | FDouble, other when is_complex other -> FComplexDouble |
| 85 | + | other, FDouble when is_complex other -> FComplexDouble |
| 86 | + | FDouble, _ | _, FDouble -> FDouble |
| 87 | + | FFloat, FFloat -> FFloat in |
| 88 | + |
| 89 | + let convert_int (ik1: ikind) (ik2: ikind): ikind = |
| 90 | + match ik1, ik2 with |
| 91 | + | IInt, ILong | ILong, IInt -> ILong |
| 92 | + | IInt, ILongLong | ILongLong, IInt -> ILongLong |
| 93 | + | ILong, ILongLong | ILongLong, ILong -> ILongLong |
| 94 | + | IUInt, IULong | IULong, IUInt -> IULong |
| 95 | + | _, IULongLong | IULongLong, _ -> IULongLong |
| 96 | + | IInt, IUInt | IUInt, IInt -> IUInt |
| 97 | + | ILong, IULong | IULong, ILong -> IULong |
| 98 | + | IInt, IULong | IULong, IInt -> IULong |
| 99 | + | ILong, IUInt | IUInt, ILong -> ILong |
| 100 | + | ILongLong, IUInt | IUInt, ILongLong -> ILongLong |
| 101 | + | IUInt128, _ | _, IUInt128 -> IUInt128 |
| 102 | + | IInt128, _ | _, IInt128 -> IInt128 |
| 103 | + | _ -> |
| 104 | + (* this should not happen *) |
| 105 | + raise |
| 106 | + (CCHFailure |
| 107 | + (LBLOCK [ |
| 108 | + STR "Missing case in integer promotion: "; |
| 109 | + STR (int_type_to_string ik1); |
| 110 | + STR " and "; |
| 111 | + STR (int_type_to_string ik2)])) in |
| 112 | + |
| 113 | + match ty1, ty2 with |
| 114 | + | TFloat (fk1, _), TFloat (fk2, _) -> Ok (TFloat (convert_float fk1 fk2, [])) |
| 115 | + | TFloat _, _ -> Ok ty1 |
| 116 | + | _, TFloat _ -> Ok ty2 |
| 117 | + | _ -> |
| 118 | + let pty1 = get_integer_promotion ty1 in |
| 119 | + let pty2 = get_integer_promotion ty2 in |
| 120 | + match pty1, pty2 with |
| 121 | + | TInt (ik1, _), TInt (ik2, _) when ik1 = ik2 -> Ok (TInt (ik1, [])) |
| 122 | + | TInt (ik1, _), TInt (ik2, _) -> Ok (TInt (convert_int ik1 ik2, [])) |
| 123 | + | _ -> |
| 124 | + Error [(elocm __LINE__) |
| 125 | + ^ "not an arithmetic type: " |
| 126 | + ^ (p2s (typ_to_pretty ty1)) |
| 127 | + ^ " or " |
| 128 | + ^ (p2s (typ_to_pretty ty2))] |
0 commit comments