summaryrefslogtreecommitdiff
path: root/gcc/rust/typecheck/rust-casts.cc
blob: e379216b94804822325f4491cc9b805c16edf82e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright (C) 2020-2023 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

#include "rust-casts.h"

namespace Rust {
namespace Resolver {

TypeCastRules::TypeCastRules (Location locus, TyTy::TyWithLocation from,
			      TyTy::TyWithLocation to)
  : locus (locus), from (from), to (to)
{}

TypeCoercionRules::CoercionResult
TypeCastRules::resolve (Location locus, TyTy::TyWithLocation from,
			TyTy::TyWithLocation to)
{
  TypeCastRules cast_rules (locus, from, to);
  return cast_rules.check ();
}

TypeCoercionRules::CoercionResult
TypeCastRules::check ()
{
  // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L565-L582
  auto possible_coercion
    = TypeCoercionRules::TryCoerce (from.get_ty (), to.get_ty (), locus);
  if (!possible_coercion.is_error ())
    return possible_coercion;

  // try the simple cast rules
  auto simple_cast = cast_rules ();
  if (!simple_cast.is_error ())
    return simple_cast;

  // failed to cast
  emit_cast_error ();
  return TypeCoercionRules::CoercionResult::get_error ();
}

TypeCoercionRules::CoercionResult
TypeCastRules::cast_rules ()
{
  // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L596
  // https://github.com/rust-lang/rust/blob/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/compiler/rustc_typeck/src/check/cast.rs#L654

  rust_debug ("cast_rules from={%s} to={%s}",
	      from.get_ty ()->debug_str ().c_str (),
	      to.get_ty ()->debug_str ().c_str ());

  switch (from.get_ty ()->get_kind ())
    {
      case TyTy::TypeKind::INFER: {
	TyTy::InferType *from_infer
	  = static_cast<TyTy::InferType *> (from.get_ty ());
	switch (from_infer->get_infer_kind ())
	  {
	  case TyTy::InferType::InferTypeKind::GENERAL:
	    return TypeCoercionRules::CoercionResult{{},
						     to.get_ty ()->clone ()};

	  case TyTy::InferType::InferTypeKind::INTEGRAL:
	    switch (to.get_ty ()->get_kind ())
	      {
	      case TyTy::TypeKind::CHAR:
	      case TyTy::TypeKind::BOOL:
	      case TyTy::TypeKind::USIZE:
	      case TyTy::TypeKind::ISIZE:
	      case TyTy::TypeKind::UINT:
	      case TyTy::TypeKind::INT:
	      case TyTy::TypeKind::POINTER:
		return TypeCoercionRules::CoercionResult{
		  {}, to.get_ty ()->clone ()};

		case TyTy::TypeKind::INFER: {
		  TyTy::InferType *to_infer
		    = static_cast<TyTy::InferType *> (to.get_ty ());

		  switch (to_infer->get_infer_kind ())
		    {
		    case TyTy::InferType::InferTypeKind::GENERAL:
		    case TyTy::InferType::InferTypeKind::INTEGRAL:
		      return TypeCoercionRules::CoercionResult{
			{}, to.get_ty ()->clone ()};

		    default:
		      return TypeCoercionRules::CoercionResult::get_error ();
		    }
		}
		break;

	      default:
		return TypeCoercionRules::CoercionResult::get_error ();
	      }
	    break;

	  case TyTy::InferType::InferTypeKind::FLOAT:
	    switch (to.get_ty ()->get_kind ())
	      {
	      case TyTy::TypeKind::USIZE:
	      case TyTy::TypeKind::ISIZE:
	      case TyTy::TypeKind::UINT:
	      case TyTy::TypeKind::INT:
		return TypeCoercionRules::CoercionResult{
		  {}, to.get_ty ()->clone ()};

		case TyTy::TypeKind::INFER: {
		  TyTy::InferType *to_infer
		    = static_cast<TyTy::InferType *> (to.get_ty ());

		  switch (to_infer->get_infer_kind ())
		    {
		    case TyTy::InferType::InferTypeKind::GENERAL:
		    case TyTy::InferType::InferTypeKind::FLOAT:
		      return TypeCoercionRules::CoercionResult{
			{}, to.get_ty ()->clone ()};

		    default:
		      return TypeCoercionRules::CoercionResult::get_error ();
		    }
		}
		break;

	      default:
		return TypeCoercionRules::CoercionResult::get_error ();
	      }
	    break;
	  }
      }
      break;

    case TyTy::TypeKind::BOOL:
      switch (to.get_ty ()->get_kind ())
	{
	case TyTy::TypeKind::INFER:
	case TyTy::TypeKind::USIZE:
	case TyTy::TypeKind::ISIZE:
	case TyTy::TypeKind::UINT:
	case TyTy::TypeKind::INT:
	  return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};

	default:
	  return TypeCoercionRules::CoercionResult::get_error ();
	}
      break;

    case TyTy::TypeKind::CHAR:
    case TyTy::TypeKind::USIZE:
    case TyTy::TypeKind::ISIZE:
    case TyTy::TypeKind::UINT:
    case TyTy::TypeKind::INT:
      switch (to.get_ty ()->get_kind ())
	{
	  case TyTy::TypeKind::CHAR: {
	    // only u8 and char
	    bool was_uint = from.get_ty ()->get_kind () == TyTy::TypeKind::UINT;
	    bool was_u8 = was_uint
			  && (static_cast<TyTy::UintType *> (from.get_ty ())
				->get_uint_kind ()
			      == TyTy::UintType::UintKind::U8);
	    if (was_u8)
	      return TypeCoercionRules::CoercionResult{{},
						       to.get_ty ()->clone ()};
	  }
	  break;

	case TyTy::TypeKind::INFER:
	case TyTy::TypeKind::USIZE:
	case TyTy::TypeKind::ISIZE:
	case TyTy::TypeKind::UINT:
	case TyTy::TypeKind::INT:
	  return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};

	default:
	  return TypeCoercionRules::CoercionResult::get_error ();
	}
      break;

    case TyTy::TypeKind::FLOAT:
      switch (to.get_ty ()->get_kind ())
	{
	case TyTy::TypeKind::FLOAT:
	  return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};

	  case TyTy::TypeKind::INFER: {
	    TyTy::InferType *to_infer
	      = static_cast<TyTy::InferType *> (to.get_ty ());

	    switch (to_infer->get_infer_kind ())
	      {
	      case TyTy::InferType::InferTypeKind::GENERAL:
	      case TyTy::InferType::InferTypeKind::FLOAT:
		return TypeCoercionRules::CoercionResult{
		  {}, to.get_ty ()->clone ()};

	      default:
		return TypeCoercionRules::CoercionResult::get_error ();
	      }
	  }
	  break;

	default:
	  return TypeCoercionRules::CoercionResult::get_error ();
	}
      break;

    case TyTy::TypeKind::REF:
    case TyTy::TypeKind::POINTER:
      switch (to.get_ty ()->get_kind ())
	{
	case TyTy::TypeKind::REF:
	case TyTy::TypeKind::POINTER:
	  return check_ptr_ptr_cast ();

	  // FIXME can you cast a pointer to a integral type?

	default:
	  return TypeCoercionRules::CoercionResult::get_error ();
	}
      break;

    default:
      return TypeCoercionRules::CoercionResult::get_error ();
    }

  return TypeCoercionRules::CoercionResult::get_error ();
}

TypeCoercionRules::CoercionResult
TypeCastRules::check_ptr_ptr_cast ()
{
  rust_debug ("check_ptr_ptr_cast from={%s} to={%s}",
	      from.get_ty ()->debug_str ().c_str (),
	      to.get_ty ()->debug_str ().c_str ());

  bool from_is_ref = from.get_ty ()->get_kind () == TyTy::TypeKind::REF;
  bool to_is_ref = to.get_ty ()->get_kind () == TyTy::TypeKind::REF;
  bool from_is_ptr = from.get_ty ()->get_kind () == TyTy::TypeKind::POINTER;
  bool to_is_ptr = to.get_ty ()->get_kind () == TyTy::TypeKind::POINTER;

  if (from_is_ptr && to_is_ptr)
    {
      // mutability is ignored here as all pointer usage requires unsafe
      return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};
    }
  else if (from_is_ref && to_is_ref)
    {
      // mutability must be coercedable
      TyTy::ReferenceType &f
	= static_cast<TyTy::ReferenceType &> (*from.get_ty ());
      TyTy::ReferenceType &t
	= static_cast<TyTy::ReferenceType &> (*to.get_ty ());

      if (TypeCoercionRules::coerceable_mutability (f.mutability (),
						    t.mutability ()))
	{
	  return TypeCoercionRules::CoercionResult{{}, to.get_ty ()->clone ()};
	}
    }

  return TypeCoercionRules::CoercionResult::get_error ();
}

void
TypeCastRules::emit_cast_error () const
{
  // error[E0604]
  RichLocation r (locus);
  r.add_range (from.get_locus ());
  r.add_range (to.get_locus ());
  rust_error_at (r, "invalid cast %<%s%> to %<%s%>",
		 from.get_ty ()->get_name ().c_str (),
		 to.get_ty ()->get_name ().c_str ());
}

} // namespace Resolver
} // namespace Rust