| 1 | /* Declarations relating to classes for reporting type mismatches. |
| 2 | Copyright (C) 2014-2025 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GCC. |
| 5 | |
| 6 | GCC is free software; you can redistribute it and/or modify it under |
| 7 | the terms of the GNU General Public License as published by the Free |
| 8 | Software Foundation; either version 3, or (at your option) any later |
| 9 | version. |
| 10 | |
| 11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GCC; see the file COPYING3. If not see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #ifndef GCC_C_TYPE_MISMATCH_H |
| 21 | #define GCC_C_TYPE_MISMATCH_H |
| 22 | |
| 23 | #include "gcc-rich-location.h" |
| 24 | |
| 25 | /* Concrete subclass of libcpp's range_label for use in |
| 26 | diagnostics involving mismatched types. |
| 27 | |
| 28 | Each frontend that uses this should supply its own implementation. |
| 29 | |
| 30 | Generate a label describing LABELLED_TYPE. The frontend may use |
| 31 | OTHER_TYPE where appropriate for highlighting the differences between |
| 32 | the two types (analogous to C++'s use of %H and %I with |
| 33 | template types). |
| 34 | |
| 35 | Either or both of LABELLED_TYPE and OTHER_TYPE may be NULL_TREE. |
| 36 | If LABELLED_TYPE is NULL_TREE, then there is no label. |
| 37 | |
| 38 | For example, this rich_location could use two instances of |
| 39 | range_label_for_type_mismatch: |
| 40 | |
| 41 | printf ("arg0: %i arg1: %s arg2: %i", |
| 42 | ^~ |
| 43 | | |
| 44 | const char * |
| 45 | 100, 101, 102); |
| 46 | ~~~ |
| 47 | | |
| 48 | int |
| 49 | |
| 50 | (a) the label for "%s" with LABELLED_TYPE for "const char*" and |
| 51 | (b) the label for "101" with LABELLED TYPE for "int" |
| 52 | where each one uses the other's type as OTHER_TYPE. */ |
| 53 | |
| 54 | class range_label_for_type_mismatch : public range_label |
| 55 | { |
| 56 | public: |
| 57 | range_label_for_type_mismatch (tree labelled_type, tree other_type) |
| 58 | : m_labelled_type (labelled_type), m_other_type (other_type) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | label_text get_text (unsigned range_idx) const override; |
| 63 | |
| 64 | protected: |
| 65 | tree m_labelled_type; |
| 66 | tree m_other_type; |
| 67 | }; |
| 68 | |
| 69 | /* Subclass of range_label for labelling the type of EXPR when reporting |
| 70 | a type mismatch between EXPR and OTHER_EXPR. |
| 71 | Either or both of EXPR and OTHER_EXPR could be NULL. */ |
| 72 | |
| 73 | class maybe_range_label_for_tree_type_mismatch : public range_label |
| 74 | { |
| 75 | public: |
| 76 | maybe_range_label_for_tree_type_mismatch (tree expr, tree other_expr) |
| 77 | : m_expr (expr), m_other_expr (other_expr) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | label_text get_text (unsigned range_idx) const final override; |
| 82 | |
| 83 | private: |
| 84 | tree m_expr; |
| 85 | tree m_other_expr; |
| 86 | }; |
| 87 | |
| 88 | class op_location_t; |
| 89 | |
| 90 | /* A subclass of rich_location for showing problems with binary operations. |
| 91 | |
| 92 | If enough location information is available, the ctor will make a |
| 93 | 3-location rich_location of the form: |
| 94 | |
| 95 | arg_0 op arg_1 |
| 96 | ~~~~~ ^~ ~~~~~ |
| 97 | | | |
| 98 | | arg1 type |
| 99 | arg0 type |
| 100 | |
| 101 | labelling the types of the arguments if SHOW_TYPES is true, |
| 102 | and using highlight_colors::lhs and highlight_colors::rhs for the ranges. |
| 103 | |
| 104 | Otherwise, it will fall back to a 1-location rich_location using the |
| 105 | compound location within LOC: |
| 106 | |
| 107 | arg_0 op arg_1 |
| 108 | ~~~~~~^~~~~~~~ |
| 109 | |
| 110 | for which we can't label the types. */ |
| 111 | |
| 112 | class binary_op_rich_location : public gcc_rich_location |
| 113 | { |
| 114 | public: |
| 115 | binary_op_rich_location (const op_location_t &loc, |
| 116 | tree arg0, tree arg1, |
| 117 | bool show_types); |
| 118 | |
| 119 | private: |
| 120 | static bool use_operator_loc_p (const op_location_t &loc, |
| 121 | tree arg0, tree arg1); |
| 122 | |
| 123 | maybe_range_label_for_tree_type_mismatch m_label_for_arg0; |
| 124 | maybe_range_label_for_tree_type_mismatch m_label_for_arg1; |
| 125 | }; |
| 126 | |
| 127 | #endif /* GCC_C_TYPE_MISMATCH_H */ |
| 128 | |