aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/backend/rust-compile.cc
blob: 3aee807109425cde6ea7e91c54b6043c0e9d7a6b (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
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
// Copyright (C) 2020-2024 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-compile.h"
#include "libproc_macro_internal/proc_macro.h"
#include "rust-compile-item.h"
#include "rust-compile-implitem.h"
#include "rust-hir-type-bounds.h"
#include "rust-compile-type.h"
#include "rust-substitution-mapper.h"
#include "rust-type-util.h"
#include "rust-session-manager.h"

namespace Rust {
namespace Compile {

const std::string GCCRS_PROC_MACRO_SYMBOL_PREFIX = "__gccrs_proc_macro_";

CompileCrate::CompileCrate (HIR::Crate &crate, Context *ctx)
  : crate (crate), ctx (ctx)
{}

CompileCrate::~CompileCrate () {}

void
CompileCrate::Compile (HIR::Crate &crate, Context *ctx)
{
  CompileCrate c (crate, ctx);
  c.go ();
}

void
CompileCrate::go ()
{
  for (auto &item : crate.get_items ())
    CompileItem::compile (item.get (), ctx);
  auto crate_type
    = Rust::Session::get_instance ().options.target_data.get_crate_type ();
  if (crate_type == TargetOptions::CrateType::PROC_MACRO)
    add_proc_macro_symbols ();
}

// This namespace brings multiple function to build and initialize multiple
// structures that needs to get exposed in the final shared object binary.
namespace {

tree
build_attribute_array (std::vector<std::string> attributes)
{
  tree attribute_ptr = build_pointer_type (char_type_node);
  tree attribute_type = build_qualified_type (attribute_ptr, TYPE_QUAL_CONST);
  return build_array_type_nelts (attribute_type, attributes.size ());
}

// We're constructing the following structure:
//
// struct {
//     const char *trait_name;
//     const char **attributes;
//     std::uint64_t attr_size;
//     TokenStream (fndecl*) (TokenStream);
// }
tree
build_derive_proc_macro ()
{
  tree char_ptr = build_pointer_type (char_type_node);
  tree const_char_type = build_qualified_type (char_ptr, TYPE_QUAL_CONST);
  Backend::typed_identifier name_field
    = Backend::typed_identifier ("trait_name", const_char_type,
				 BUILTINS_LOCATION);

  tree handle_ptr = build_pointer_type (void_type_node);
  Backend::typed_identifier fndecl_field
    = Backend::typed_identifier ("fndecl", handle_ptr, BUILTINS_LOCATION);

  tree attribute_ptr = build_pointer_type (const_ptr_type_node);
  Backend::typed_identifier attributes_field
    = Backend::typed_identifier ("attributes", attribute_ptr,
				 BUILTINS_LOCATION);

  Backend::typed_identifier size_field
    = Backend::typed_identifier ("attr_size", unsigned_type_node,
				 BUILTINS_LOCATION);

  return Backend::struct_type (
    {name_field, attributes_field, size_field, fndecl_field});
}

// We're constructing the following structure:
//
// struct {
//     const char *name;
//     TokenStream (fndecl*) (TokenStream);
// }
tree
build_bang_proc_macro ()
{
  tree char_ptr = build_pointer_type (char_type_node);
  tree const_char_type = build_qualified_type (char_ptr, TYPE_QUAL_CONST);
  Backend::typed_identifier name_field
    = Backend::typed_identifier ("name", const_char_type, BUILTINS_LOCATION);

  tree handle_ptr = ptr_type_node;
  Backend::typed_identifier fndecl_field
    = Backend::typed_identifier ("fndecl", handle_ptr, BUILTINS_LOCATION);

  return Backend::struct_type ({name_field, fndecl_field});
}

// Bang proc macros and attribute proc macros almost have the same members
// the function pointer type is not the same.
//
// We're constructing the following structure:
//
// struct {
//     const char *name;
//     TokenStream (fndecl*) (TokenStream, TokenStream);
// }
tree
build_attribute_proc_macro ()
{
  return build_bang_proc_macro ();
}

tree
build_proc_macro_payload ()
{
  tree bang = build_bang_proc_macro ();
  tree attribute = build_attribute_proc_macro ();
  tree derive = build_derive_proc_macro ();

  Backend::typed_identifier bang_field
    = Backend::typed_identifier ("bang", bang, BUILTINS_LOCATION);
  Backend::typed_identifier attribute_field
    = Backend::typed_identifier ("attribute", attribute, BUILTINS_LOCATION);
  Backend::typed_identifier derive_field
    = Backend::typed_identifier ("custom_derive", derive, BUILTINS_LOCATION);

  // We rely on the tag to represent the index of any union member. This means
  // we should keep those fields in the same order as the tag representation for
  // it to be kept in sync.
  // Hence why the following code exist: to keep in sync the field vector and
  // the tag enumeration.
  std::vector<Backend::typed_identifier> fields;
  fields.insert (fields.begin () + ProcMacro::CUSTOM_DERIVE, derive_field);
  fields.insert (fields.begin () + ProcMacro::ATTR, attribute_field);
  fields.insert (fields.begin () + ProcMacro::BANG, bang_field);

  return Backend::union_type (fields);
}

// Build the tagged union proc macro type
//
// struct {
//     unsigned short tag;
//     union { BangProcMacro , DeriveProcMacro, AttributeProcMacro} payload;
// }
tree
build_proc_macro ()
{
  auto union_field = build_proc_macro_payload ();
  Backend::typed_identifier payload_field
    = Backend::typed_identifier ("payload", union_field, BUILTINS_LOCATION);

  Backend::typed_identifier tag_field
    = Backend::typed_identifier ("tag", short_unsigned_type_node,
				 BUILTINS_LOCATION);

  return Backend::struct_type ({tag_field, payload_field});
}

tree
build_proc_macro_buffer (tree proc_macro_type, size_t total_macro)
{
  return build_array_type_nelts (proc_macro_type, total_macro);
}

tree
build_entrypoint (tree proc_macro_buffer)
{
  return build_reference_type_for_mode (proc_macro_buffer, E_VOIDmode, false);
}

tree
init_derive_proc_macro (Context *ctx, CustomDeriveInfo infos)
{
  tree derive_proc_macro_type = build_derive_proc_macro ();
  tree trait_name = build_string_literal (infos.trait_name.c_str ());

  tree attribute_ptr;
  if (infos.attributes.size () == 0)
    {
      attribute_ptr = HIRCompileBase::address_expression (null_pointer_node,
							  BUILTINS_LOCATION);
    }
  else
    {
      tree attribute_array_type = build_attribute_array (infos.attributes);

      std::vector<tree> attr_ctors;
      std::vector<unsigned long> indices;

      size_t index = 0;
      for (auto &attr : infos.attributes)
	{
	  attr_ctors.push_back (build_string_literal (attr.c_str ()));
	  indices.push_back (index);
	  index++;
	}

      tree attributes
	= Backend::array_constructor_expression (attribute_array_type, indices,
						 attr_ctors, BUILTINS_LOCATION);

      std::string attribute_var_name
	= GCCRS_PROC_MACRO_SYMBOL_PREFIX + infos.trait_name;
      Bvariable *attributes_var
	= Backend::global_variable (attribute_var_name.c_str (),
				    attribute_var_name.c_str (),
				    attribute_array_type, false /* internal */,
				    true /* hidden */, false /* no gc */,
				    BUILTINS_LOCATION);
      Backend::global_variable_set_init (attributes_var, attributes);
      ctx->push_var (attributes_var);

      attribute_ptr
	= HIRCompileBase::address_expression (attributes_var->get_decl (),
					      BUILTINS_LOCATION);
    }

  tree attr_size = build_int_cst (unsigned_type_node, infos.attributes.size ());

  tree handle
    = HIRCompileBase::address_expression (infos.fndecl, BUILTINS_LOCATION);

  return Backend::constructor_expression (derive_proc_macro_type, false,
					  {trait_name, attribute_ptr, attr_size,
					   handle},
					  -1 /* Structure: no index */,
					  BUILTINS_LOCATION);
}

tree
init_attribute_proc_macro (tree macro)
{
  tree attribute_proc_macro_type = build_attribute_proc_macro ();
  tree macro_name
    = build_string_literal (IDENTIFIER_POINTER (DECL_NAME (macro)));
  tree handle = HIRCompileBase::address_expression (macro, BUILTINS_LOCATION);

  return Backend::constructor_expression (attribute_proc_macro_type, false,
					  {macro_name, handle},
					  -1 /* Structure: No index */,
					  BUILTINS_LOCATION);
}

tree
init_bang_proc_macro (tree macro)
{
  // Attribute and bang proc macros have the same structure, they can be
  // initialized with the same code.
  return init_attribute_proc_macro (macro);
}

tree
init_proc_macro (tree payload, tree proc_macro_type,
		 ProcMacro::ProcmacroTag tag)
{
  auto discriminant = static_cast<int> (tag);

  tree macro_tag = build_int_cst (short_unsigned_type_node, discriminant);

  tree payload_union
    = Backend::constructor_expression (build_proc_macro_payload (), false,
				       {payload},
				       discriminant /* Union: member index */,
				       BUILTINS_LOCATION);

  return Backend::constructor_expression (proc_macro_type,
					  false /* invariant */,
					  {macro_tag, payload_union},
					  -1 /* Structure: No index */,
					  BUILTINS_LOCATION);
}

tree
initialize_proc_macro_array (Context *ctx, tree proc_macro_buffer_type,
			     tree proc_macro_type)
{
  std::vector<unsigned long> indexes;
  std::vector<tree> ctors;
  size_t index = 0;
  for (auto &macro : ctx->get_derive_proc_macros ())
    {
      tree proc_macro = init_derive_proc_macro (ctx, macro);
      ctors.push_back (
	init_proc_macro (proc_macro, proc_macro_type,
			 ProcMacro::ProcmacroTag::CUSTOM_DERIVE));
      indexes.push_back (index);
      index++;
    }
  for (auto &macro : ctx->get_attribute_proc_macros ())
    {
      tree proc_macro = init_attribute_proc_macro (macro);

      ctors.push_back (init_proc_macro (proc_macro, proc_macro_type,
					ProcMacro::ProcmacroTag::ATTR));
      indexes.push_back (index);
      index++;
    }
  for (auto &macro : ctx->get_bang_proc_macros ())
    {
      tree proc_macro = init_bang_proc_macro (macro);

      ctors.push_back (init_proc_macro (proc_macro, proc_macro_type,
					ProcMacro::ProcmacroTag::BANG));
      indexes.push_back (index);
      index++;
    }

  return Backend::array_constructor_expression (proc_macro_buffer_type, indexes,
						ctors, BUILTINS_LOCATION);
}

} // namespace

void
CompileCrate::add_proc_macro_symbols ()
{
  auto total_macros = ctx->get_attribute_proc_macros ().size ()
		      + ctx->get_bang_proc_macros ().size ()
		      + ctx->get_derive_proc_macros ().size ();

  tree proc_macro_type = build_proc_macro ();
  tree proc_macro_buffer_type
    = build_proc_macro_buffer (proc_macro_type, total_macros);
  tree entrypoint_type = build_entrypoint (proc_macro_buffer_type);

  std::string decl_symbol_name = generate_proc_macro_decls_symbol (
    0 /* FIXME: Change to stable crate id */);

  Bvariable *macro_decls
    = Backend::global_variable (decl_symbol_name.c_str (),
				decl_symbol_name.c_str (), entrypoint_type,
				false /* internal */, false /* not hidden */,
				false /* no gc */, BUILTINS_LOCATION);

  std::string buffer_name
    = GCCRS_PROC_MACRO_SYMBOL_PREFIX + "proc_macro_buffer";

  Bvariable *proc_macro_buffer
    = Backend::global_variable (buffer_name.c_str (), buffer_name.c_str (),
				proc_macro_buffer_type, false /* internal */,
				true /* hidden */, false /* no gc */,
				BUILTINS_LOCATION);
  Backend::global_variable_set_init (
    proc_macro_buffer,
    initialize_proc_macro_array (ctx, proc_macro_buffer_type, proc_macro_type));
  ctx->push_var (proc_macro_buffer);

  Backend::global_variable_set_init (
    macro_decls,
    HIRCompileBase::address_expression (proc_macro_buffer->get_decl (),
					BUILTINS_LOCATION));

  ctx->push_var (macro_decls);
}

// Shared methods in compilation

tree
HIRCompileBase::coercion_site (HirId id, tree rvalue, TyTy::BaseType *rval,
			       TyTy::BaseType *lval, location_t lvalue_locus,
			       location_t rvalue_locus)
{
  std::vector<Resolver::Adjustment> *adjustments = nullptr;
  bool ok = ctx->get_tyctx ()->lookup_autoderef_mappings (id, &adjustments);
  if (ok)
    {
      rvalue = resolve_adjustements (*adjustments, rvalue, rvalue_locus);
    }

  return coercion_site1 (rvalue, rval, lval, lvalue_locus, rvalue_locus);
}

tree
HIRCompileBase::coercion_site1 (tree rvalue, TyTy::BaseType *rval,
				TyTy::BaseType *lval, location_t lvalue_locus,
				location_t rvalue_locus)
{
  if (rvalue == error_mark_node)
    return error_mark_node;

  TyTy::BaseType *actual = rval->destructure ();
  TyTy::BaseType *expected = lval->destructure ();

  if (expected->get_kind () == TyTy::TypeKind::REF)
    {
      // this is a dyn object
      if (RS_DST_FLAG_P (TREE_TYPE (rvalue)))
	{
	  return rvalue;
	}

      // bad coercion... of something to a reference
      if (actual->get_kind () != TyTy::TypeKind::REF)
	return error_mark_node;

      const TyTy::ReferenceType *exp
	= static_cast<const TyTy::ReferenceType *> (expected);
      const TyTy::ReferenceType *act
	= static_cast<const TyTy::ReferenceType *> (actual);

      tree deref_rvalue = indirect_expression (rvalue, rvalue_locus);
      tree coerced
	= coercion_site1 (deref_rvalue, act->get_base (), exp->get_base (),
			  lvalue_locus, rvalue_locus);
      if (exp->is_dyn_object () && RS_DST_FLAG_P (TREE_TYPE (coerced)))
	return coerced;

      return address_expression (coerced, rvalue_locus);
    }
  else if (expected->get_kind () == TyTy::TypeKind::POINTER)
    {
      // this is a dyn object
      if (RS_DST_FLAG_P (TREE_TYPE (rvalue)))
	{
	  return rvalue;
	}

      // bad coercion... of something to a reference
      bool valid_coercion = actual->get_kind () == TyTy::TypeKind::REF
			    || actual->get_kind () == TyTy::TypeKind::POINTER;
      if (!valid_coercion)
	return error_mark_node;

      const TyTy::PointerType *exp
	= static_cast<const TyTy::PointerType *> (expected);

      TyTy::BaseType *actual_base = nullptr;
      if (actual->get_kind () == TyTy::TypeKind::REF)
	{
	  const TyTy::ReferenceType *act
	    = static_cast<const TyTy::ReferenceType *> (actual);

	  actual_base = act->get_base ();
	}
      else if (actual->get_kind () == TyTy::TypeKind::POINTER)
	{
	  const TyTy::PointerType *act
	    = static_cast<const TyTy::PointerType *> (actual);

	  actual_base = act->get_base ();
	}
      rust_assert (actual_base != nullptr);

      tree deref_rvalue = indirect_expression (rvalue, rvalue_locus);
      tree coerced
	= coercion_site1 (deref_rvalue, actual_base, exp->get_base (),
			  lvalue_locus, rvalue_locus);

      if (exp->is_dyn_object () && RS_DST_FLAG_P (TREE_TYPE (coerced)))
	return coerced;

      return address_expression (coerced, rvalue_locus);
    }
  else if (expected->get_kind () == TyTy::TypeKind::ARRAY)
    {
      if (actual->get_kind () != TyTy::TypeKind::ARRAY)
	return error_mark_node;

      tree tree_rval_type = TyTyResolveCompile::compile (ctx, actual);
      tree tree_lval_type = TyTyResolveCompile::compile (ctx, expected);
      if (!verify_array_capacities (tree_lval_type, tree_rval_type,
				    lvalue_locus, rvalue_locus))
	return error_mark_node;
    }
  else if (expected->get_kind () == TyTy::TypeKind::SLICE)
    {
      // bad coercion
      bool valid_coercion = actual->get_kind () == TyTy::TypeKind::SLICE
			    || actual->get_kind () == TyTy::TypeKind::ARRAY;
      if (!valid_coercion)
	return error_mark_node;

      // nothing to do here
      if (actual->get_kind () == TyTy::TypeKind::SLICE)
	return rvalue;

      // return an unsized coercion
      Resolver::Adjustment unsize_adj (
	Resolver::Adjustment::AdjustmentType::UNSIZE, actual, expected);
      return resolve_unsized_adjustment (unsize_adj, rvalue, rvalue_locus);
    }

  return rvalue;
}

tree
HIRCompileBase::coerce_to_dyn_object (tree compiled_ref,
				      const TyTy::BaseType *actual,
				      const TyTy::DynamicObjectType *ty,
				      location_t locus)
{
  // DST's get wrapped in a pseudo reference that doesnt exist...
  const TyTy::ReferenceType r (ctx->get_mappings ()->get_next_hir_id (),
			       TyTy::TyVar (ty->get_ref ()), Mutability::Imm);

  tree dynamic_object = TyTyResolveCompile::compile (ctx, &r);
  tree dynamic_object_fields = TYPE_FIELDS (dynamic_object);
  tree vtable_field = DECL_CHAIN (dynamic_object_fields);
  rust_assert (TREE_CODE (TREE_TYPE (vtable_field)) == ARRAY_TYPE);

  //' this assumes ordering and current the structure is
  // __trait_object_ptr
  // [list of function ptrs]

  std::vector<std::pair<Resolver::TraitReference *, HIR::ImplBlock *>>
    probed_bounds_for_receiver = Resolver::TypeBoundsProbe::Probe (actual);

  tree address_of_compiled_ref = null_pointer_node;
  if (!actual->is_unit ())
    address_of_compiled_ref = address_expression (compiled_ref, locus);

  std::vector<tree> vtable_ctor_elems;
  std::vector<unsigned long> vtable_ctor_idx;
  unsigned long i = 0;
  for (auto &bound : ty->get_object_items ())
    {
      const Resolver::TraitItemReference *item = bound.first;
      const TyTy::TypeBoundPredicate *predicate = bound.second;

      auto address = compute_address_for_trait_item (item, predicate,
						     probed_bounds_for_receiver,
						     actual, actual, locus);
      vtable_ctor_elems.push_back (address);
      vtable_ctor_idx.push_back (i++);
    }

  tree vtable_ctor
    = Backend::array_constructor_expression (TREE_TYPE (vtable_field),
					     vtable_ctor_idx, vtable_ctor_elems,
					     locus);

  std::vector<tree> dyn_ctor = {address_of_compiled_ref, vtable_ctor};
  return Backend::constructor_expression (dynamic_object, false, dyn_ctor, -1,
					  locus);
}

tree
HIRCompileBase::compute_address_for_trait_item (
  const Resolver::TraitItemReference *ref,
  const TyTy::TypeBoundPredicate *predicate,
  std::vector<std::pair<Resolver::TraitReference *, HIR::ImplBlock *>>
    &receiver_bounds,
  const TyTy::BaseType *receiver, const TyTy::BaseType *root, location_t locus)
{
  // There are two cases here one where its an item which has an implementation
  // within a trait-impl-block. Then there is the case where there is a default
  // implementation for this within the trait.
  //
  // The awkward part here is that this might be a generic trait and we need to
  // figure out the correct monomorphized type for this so we can resolve the
  // address of the function , this is stored as part of the
  // type-bound-predicate
  //
  // Algo:
  // check if there is an impl-item for this trait-item-ref first
  // else assert that the trait-item-ref has an implementation
  //
  // FIXME this does not support super traits

  TyTy::TypeBoundPredicateItem predicate_item
    = predicate->lookup_associated_item (ref->get_identifier ());
  rust_assert (!predicate_item.is_error ());

  // this is the expected end type
  TyTy::BaseType *trait_item_type = predicate_item.get_tyty_for_receiver (root);
  rust_assert (trait_item_type->get_kind () == TyTy::TypeKind::FNDEF);
  TyTy::FnType *trait_item_fntype
    = static_cast<TyTy::FnType *> (trait_item_type);

  // find impl-block for this trait-item-ref
  HIR::ImplBlock *associated_impl_block = nullptr;
  const Resolver::TraitReference *predicate_trait_ref = predicate->get ();
  for (auto &item : receiver_bounds)
    {
      Resolver::TraitReference *trait_ref = item.first;
      HIR::ImplBlock *impl_block = item.second;
      if (predicate_trait_ref->is_equal (*trait_ref))
	{
	  associated_impl_block = impl_block;
	  break;
	}
    }

  // FIXME this probably should just return error_mark_node but this helps
  // debug for now since we are wrongly returning early on type-resolution
  // failures, until we take advantage of more error types and error_mark_node
  rust_assert (associated_impl_block != nullptr);

  // lookup self for the associated impl
  std::unique_ptr<HIR::Type> &self_type_path
    = associated_impl_block->get_type ();
  TyTy::BaseType *self = nullptr;
  bool ok = ctx->get_tyctx ()->lookup_type (
    self_type_path->get_mappings ().get_hirid (), &self);
  rust_assert (ok);

  // lookup the predicate item from the self
  TyTy::TypeBoundPredicate *self_bound = nullptr;
  for (auto &bound : self->get_specified_bounds ())
    {
      const Resolver::TraitReference *bound_ref = bound.get ();
      const Resolver::TraitReference *specified_ref = predicate->get ();
      if (bound_ref->is_equal (*specified_ref))
	{
	  self_bound = &bound;
	  break;
	}
    }
  rust_assert (self_bound != nullptr);

  // lookup the associated item from the associated impl block
  TyTy::TypeBoundPredicateItem associated_self_item
    = self_bound->lookup_associated_item (ref->get_identifier ());
  rust_assert (!associated_self_item.is_error ());

  // Lookup the impl-block for the associated impl_item if it exists
  HIR::Function *associated_function = nullptr;
  for (auto &impl_item : associated_impl_block->get_impl_items ())
    {
      bool is_function = impl_item->get_impl_item_type ()
			 == HIR::ImplItem::ImplItemType::FUNCTION;
      if (!is_function)
	continue;

      HIR::Function *fn = static_cast<HIR::Function *> (impl_item.get ());
      bool found_associated_item
	= fn->get_function_name ().as_string ().compare (ref->get_identifier ())
	  == 0;
      if (found_associated_item)
	associated_function = fn;
    }

  // we found an impl_item for this
  if (associated_function != nullptr)
    {
      // lookup the associated type for this item
      TyTy::BaseType *lookup = nullptr;
      bool ok = ctx->get_tyctx ()->lookup_type (
	associated_function->get_mappings ().get_hirid (), &lookup);
      rust_assert (ok);
      rust_assert (lookup->get_kind () == TyTy::TypeKind::FNDEF);
      TyTy::FnType *lookup_fntype = static_cast<TyTy::FnType *> (lookup);

      if (lookup_fntype->needs_substitution ())
	{
	  TyTy::BaseType *infer
	    = Resolver::SubstMapper::InferSubst (lookup_fntype, UNDEF_LOCATION);
	  infer
	    = Resolver::unify_site (infer->get_ref (),
				    TyTy::TyWithLocation (trait_item_fntype),
				    TyTy::TyWithLocation (infer),
				    UNDEF_LOCATION);
	  rust_assert (infer->get_kind () == TyTy::TypeKind::FNDEF);
	  lookup_fntype = static_cast<TyTy::FnType *> (infer);
	}

      return CompileInherentImplItem::Compile (associated_function, ctx,
					       lookup_fntype, true, locus);
    }

  // we can only compile trait-items with a body
  bool trait_item_has_definition = ref->is_optional ();
  rust_assert (trait_item_has_definition);

  HIR::TraitItem *trait_item = ref->get_hir_trait_item ();
  return CompileTraitItem::Compile (trait_item, ctx, trait_item_fntype, true,
				    locus);
}

bool
HIRCompileBase::verify_array_capacities (tree ltype, tree rtype,
					 location_t lvalue_locus,
					 location_t rvalue_locus)
{
  rust_assert (ltype != NULL_TREE);
  rust_assert (rtype != NULL_TREE);

  // lets just return ok as other errors have already occurred
  if (ltype == error_mark_node || rtype == error_mark_node)
    return true;

  tree ltype_domain = TYPE_DOMAIN (ltype);
  if (!ltype_domain)
    return false;

  if (!TREE_CONSTANT (TYPE_MAX_VALUE (ltype_domain)))
    return false;

  unsigned HOST_WIDE_INT ltype_length
    = wi::ext (wi::to_offset (TYPE_MAX_VALUE (ltype_domain))
		 - wi::to_offset (TYPE_MIN_VALUE (ltype_domain)) + 1,
	       TYPE_PRECISION (TREE_TYPE (ltype_domain)),
	       TYPE_SIGN (TREE_TYPE (ltype_domain)))
	.to_uhwi ();

  tree rtype_domain = TYPE_DOMAIN (rtype);
  if (!rtype_domain)
    return false;

  if (!TREE_CONSTANT (TYPE_MAX_VALUE (rtype_domain)))
    return false;

  unsigned HOST_WIDE_INT rtype_length
    = wi::ext (wi::to_offset (TYPE_MAX_VALUE (rtype_domain))
		 - wi::to_offset (TYPE_MIN_VALUE (rtype_domain)) + 1,
	       TYPE_PRECISION (TREE_TYPE (rtype_domain)),
	       TYPE_SIGN (TREE_TYPE (rtype_domain)))
	.to_uhwi ();

  if (ltype_length != rtype_length)
    {
      rust_error_at (rvalue_locus, ErrorCode::E0308,
		     "mismatched types, expected an array with a fixed size "
		     "of " HOST_WIDE_INT_PRINT_UNSIGNED
		     " elements, found one with " HOST_WIDE_INT_PRINT_UNSIGNED
		     " elements",
		     ltype_length, rtype_length);
      return false;
    }

  return true;
}

} // namespace Compile
} // namespace Rust