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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
|
// Copyright (C) 2020-2021 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 "rust-compile-item.h"
#include "rust-compile-expr.h"
#include "rust-compile-struct-field-expr.h"
#include "rust-hir-trait-resolve.h"
#include "rust-hir-path-probe.h"
#include "rust-hir-dot-operator.h"
namespace Rust {
namespace Compile {
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.items)
CompileItem::compile (item.get (), ctx, false);
for (auto &item : crate.items)
CompileItem::compile (item.get (), ctx, true);
}
// rust-compile-expr.h
void
CompileExpr::visit (HIR::CallExpr &expr)
{
TyTy::BaseType *tyty = nullptr;
if (!ctx->get_tyctx ()->lookup_type (
expr.get_fnexpr ()->get_mappings ().get_hirid (), &tyty))
{
rust_error_at (expr.get_locus (), "unknown type");
return;
}
// must be a tuple constructor
bool is_fn = tyty->get_kind () == TyTy::TypeKind::FNDEF
|| tyty->get_kind () == TyTy::TypeKind::FNPTR;
if (!is_fn)
{
rust_assert (tyty->get_kind () == TyTy::TypeKind::ADT);
TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (tyty);
Btype *compiled_adt_type = TyTyResolveCompile::compile (ctx, tyty);
// this assumes all fields are in order from type resolution and if a
// base struct was specified those fields are filed via accesors
std::vector<Bexpression *> vals;
for (size_t i = 0; i < expr.get_arguments ().size (); i++)
{
auto &argument = expr.get_arguments ().at (i);
auto rvalue = CompileExpr::Compile (argument.get (), ctx);
// assignments are coercion sites so lets convert the rvalue if
// necessary
auto respective_field = adt->get_field (i);
auto expected = respective_field->get_field_type ();
TyTy::BaseType *actual = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
argument->get_mappings ().get_hirid (), &actual);
rust_assert (ok);
// coerce it if required
rvalue = coercion_site (rvalue, actual, expected, expr.get_locus ());
// add it to the list
vals.push_back (rvalue);
}
translated
= ctx->get_backend ()->constructor_expression (compiled_adt_type, vals,
-1, expr.get_locus ());
}
else
{
auto get_parameter_tyty_at_index
= [] (const TyTy::BaseType *base, size_t index,
TyTy::BaseType **result) -> bool {
bool is_fn = base->get_kind () == TyTy::TypeKind::FNDEF
|| base->get_kind () == TyTy::TypeKind::FNPTR;
rust_assert (is_fn);
if (base->get_kind () == TyTy::TypeKind::FNPTR)
{
const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (base);
*result = fn->param_at (index);
return true;
}
const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (base);
auto param = fn->param_at (index);
*result = param.second;
return true;
};
bool is_varadic = false;
if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
{
const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (tyty);
is_varadic = fn->is_varadic ();
}
size_t required_num_args;
if (tyty->get_kind () == TyTy::TypeKind::FNDEF)
{
const TyTy::FnType *fn = static_cast<const TyTy::FnType *> (tyty);
required_num_args = fn->num_params ();
}
else
{
const TyTy::FnPtr *fn = static_cast<const TyTy::FnPtr *> (tyty);
required_num_args = fn->num_params ();
}
std::vector<Bexpression *> args;
for (size_t i = 0; i < expr.get_arguments ().size (); i++)
{
auto &argument = expr.get_arguments ().at (i);
auto rvalue = CompileExpr::Compile (argument.get (), ctx);
if (is_varadic && i >= required_num_args)
{
args.push_back (rvalue);
continue;
}
// assignments are coercion sites so lets convert the rvalue if
// necessary
bool ok;
TyTy::BaseType *expected = nullptr;
ok = get_parameter_tyty_at_index (tyty, i, &expected);
rust_assert (ok);
TyTy::BaseType *actual = nullptr;
ok = ctx->get_tyctx ()->lookup_type (
argument->get_mappings ().get_hirid (), &actual);
rust_assert (ok);
// coerce it if required
rvalue = coercion_site (rvalue, actual, expected, expr.get_locus ());
// add it to the list
args.push_back (rvalue);
}
// must be a call to a function
auto fn_address = CompileExpr::Compile (expr.get_fnexpr (), ctx);
auto fncontext = ctx->peek_fn ();
translated
= ctx->get_backend ()->call_expression (fncontext.fndecl, fn_address,
args, nullptr,
expr.get_locus ());
}
}
void
CompileExpr::visit (HIR::MethodCallExpr &expr)
{
// method receiver
Bexpression *self = CompileExpr::Compile (expr.get_receiver ().get (), ctx);
// lookup the resolved name
NodeId resolved_node_id = UNKNOWN_NODEID;
if (!ctx->get_resolver ()->lookup_resolved_name (
expr.get_mappings ().get_nodeid (), &resolved_node_id))
{
rust_error_at (expr.get_locus (), "failed to lookup resolved MethodCall");
return;
}
// reverse lookup
HirId ref;
if (!ctx->get_mappings ()->lookup_node_to_hir (
expr.get_mappings ().get_crate_num (), resolved_node_id, &ref))
{
rust_fatal_error (expr.get_locus (), "reverse lookup failure");
return;
}
// lookup the expected function type
TyTy::BaseType *lookup_fntype = nullptr;
bool ok = ctx->get_tyctx ()->lookup_type (
expr.get_method_name ().get_mappings ().get_hirid (), &lookup_fntype);
rust_assert (ok);
rust_assert (lookup_fntype->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (lookup_fntype);
TyTy::BaseType *receiver = nullptr;
ok = ctx->get_tyctx ()->lookup_receiver (expr.get_mappings ().get_hirid (),
&receiver);
rust_assert (ok);
bool is_dyn_dispatch
= receiver->get_root ()->get_kind () == TyTy::TypeKind::DYNAMIC;
bool is_generic_receiver = receiver->get_kind () == TyTy::TypeKind::PARAM;
if (is_generic_receiver)
{
TyTy::ParamType *p = static_cast<TyTy::ParamType *> (receiver);
receiver = p->resolve ();
}
if (is_dyn_dispatch)
{
const TyTy::DynamicObjectType *dyn
= static_cast<const TyTy::DynamicObjectType *> (receiver->get_root ());
size_t offs = 0;
const Resolver::TraitItemReference *ref = nullptr;
for (auto &item : dyn->get_object_items ())
{
auto t = item->get_tyty ();
rust_assert (t->get_kind () == TyTy::TypeKind::FNDEF);
auto ft = static_cast<TyTy::FnType *> (t);
if (ft->get_id () == fntype->get_id ())
{
ref = item;
break;
}
offs++;
}
if (ref == nullptr)
{
translated = ctx->get_backend ()->error_expression ();
return;
}
// get any indirection sorted out
auto receiver_ref = self;
if (receiver->get_kind () == TyTy::TypeKind::REF)
{
TyTy::ReferenceType *r
= static_cast<TyTy::ReferenceType *> (receiver);
auto indirect_ty = r->get_base ();
Btype *indrect_compiled_tyty
= TyTyResolveCompile::compile (ctx, indirect_ty);
Bexpression *indirect
= ctx->get_backend ()->indirect_expression (indrect_compiled_tyty,
receiver_ref, true,
expr.get_locus ());
receiver_ref = indirect;
}
// access the offs + 1 for the fnptr and offs=0 for the reciever obj
Bexpression *self_argument
= ctx->get_backend ()->struct_field_expression (receiver_ref, 0,
expr.get_locus ());
// access the vtable for the fn
Bexpression *fn_vtable_access
= ctx->get_backend ()->struct_field_expression (receiver_ref, offs + 1,
expr.get_locus ());
// cast it to the correct fntype
Btype *expected_fntype = TyTyResolveCompile::compile (ctx, fntype, true);
Bexpression *fn_convert_expr
= ctx->get_backend ()->convert_expression (expected_fntype,
fn_vtable_access,
expr.get_locus ());
fncontext fnctx = ctx->peek_fn ();
Bblock *enclosing_scope = ctx->peek_enclosing_scope ();
bool is_address_taken = false;
Bstatement *ret_var_stmt = nullptr;
Bvariable *fn_convert_expr_tmp = ctx->get_backend ()->temporary_variable (
fnctx.fndecl, enclosing_scope, expected_fntype, fn_convert_expr,
is_address_taken, expr.get_locus (), &ret_var_stmt);
ctx->add_statement (ret_var_stmt);
std::vector<Bexpression *> args;
args.push_back (self_argument);
for (auto &argument : expr.get_arguments ())
{
Bexpression *compiled_expr
= CompileExpr::Compile (argument.get (), ctx);
args.push_back (compiled_expr);
}
Bexpression *fn_expr
= ctx->get_backend ()->var_expression (fn_convert_expr_tmp,
expr.get_locus ());
translated
= ctx->get_backend ()->call_expression (fnctx.fndecl, fn_expr, args,
nullptr, expr.get_locus ());
return;
}
// address of compiled function
Bexpression *fn_expr = ctx->get_backend ()->error_expression ();
// lookup compiled functions since it may have already been compiled
Bfunction *fn = nullptr;
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
{
fn_expr
= ctx->get_backend ()->function_code_expression (fn, expr.get_locus ());
}
else
{
// Now we can try and resolve the address since this might be a forward
// declared function, generic function which has not be compiled yet or
// its an not yet trait bound function
HIR::ImplItem *resolved_item = ctx->get_mappings ()->lookup_hir_implitem (
expr.get_mappings ().get_crate_num (), ref, nullptr);
if (resolved_item == nullptr)
{
// it might be resolved to a trait item
HIR::TraitItem *trait_item
= ctx->get_mappings ()->lookup_hir_trait_item (
expr.get_mappings ().get_crate_num (), ref);
HIR::Trait *trait = ctx->get_mappings ()->lookup_trait_item_mapping (
trait_item->get_mappings ().get_hirid ());
Resolver::TraitReference *trait_ref
= &Resolver::TraitReference::error_node ();
bool ok = ctx->get_tyctx ()->lookup_trait_reference (
trait->get_mappings ().get_defid (), &trait_ref);
rust_assert (ok);
// the type resolver can only resolve type bounds to their trait
// item so its up to us to figure out if this path should resolve
// to an trait-impl-block-item or if it can be defaulted to the
// trait-impl-item's definition
auto root = receiver->get_root ();
std::vector<Resolver::PathProbeCandidate> candidates
= Resolver::PathProbeType::Probe (
root, expr.get_method_name ().get_segment (), true, false, true);
if (candidates.size () == 0)
{
// this means we are defaulting back to the trait_item if
// possible
Resolver::TraitItemReference *trait_item_ref = nullptr;
bool ok = trait_ref->lookup_hir_trait_item (*trait_item,
&trait_item_ref);
rust_assert (ok); // found
rust_assert (trait_item_ref->is_optional ()); // has definition
// FIXME Optional means it has a definition and an associated
// block which can be a default implementation, if it does not
// contain an implementation we should actually return
// error_mark_node
TyTy::BaseType *self_type = nullptr;
if (!ctx->get_tyctx ()->lookup_type (
expr.get_receiver ()->get_mappings ().get_hirid (),
&self_type))
{
rust_error_at (expr.get_locus (),
"failed to resolve type for self param");
return;
}
fn_expr = CompileTraitItem::Compile (
self_type, trait_item_ref->get_hir_trait_item (), ctx, fntype,
true, expr.get_locus ());
}
else
{
std::vector<Resolver::Adjustment> adjustments;
Resolver::PathProbeCandidate *candidate
= Resolver::MethodResolution::Select (candidates, root,
adjustments);
// FIXME this will be a case to return error_mark_node, there is
// an error scenario where a Trait Foo has a method Bar, but this
// receiver does not implement this trait or has an incompatible
// implementation and we should just return error_mark_node
rust_assert (candidate != nullptr);
rust_assert (candidate->is_impl_candidate ());
HIR::ImplItem *impl_item = candidate->item.impl.impl_item;
TyTy::BaseType *self_type = nullptr;
if (!ctx->get_tyctx ()->lookup_type (
expr.get_receiver ()->get_mappings ().get_hirid (),
&self_type))
{
rust_error_at (expr.get_locus (),
"failed to resolve type for self param");
return;
}
if (!fntype->has_subsititions_defined ())
fn_expr
= CompileInherentImplItem::Compile (self_type, impl_item, ctx,
true);
else
fn_expr
= CompileInherentImplItem::Compile (self_type, impl_item, ctx,
true, fntype);
}
}
else
{
TyTy::BaseType *self_type = nullptr;
if (!ctx->get_tyctx ()->lookup_type (
expr.get_receiver ()->get_mappings ().get_hirid (), &self_type))
{
rust_error_at (expr.get_locus (),
"failed to resolve type for self param");
return;
}
if (!fntype->has_subsititions_defined ())
fn_expr
= CompileInherentImplItem::Compile (self_type, resolved_item, ctx,
true);
else
fn_expr
= CompileInherentImplItem::Compile (self_type, resolved_item, ctx,
true, fntype);
}
}
std::vector<Bexpression *> args;
// lookup the autoderef mappings
std::vector<Resolver::Adjustment> *adjustments = nullptr;
ok = ctx->get_tyctx ()->lookup_autoderef_mappings (
expr.get_mappings ().get_hirid (), &adjustments);
rust_assert (ok);
for (auto &adjustment : *adjustments)
{
switch (adjustment.get_type ())
{
case Resolver::Adjustment::AdjustmentType::IMM_REF:
case Resolver::Adjustment::AdjustmentType::MUT_REF:
self = ctx->get_backend ()->address_expression (
self, expr.get_receiver ()->get_locus ());
break;
case Resolver::Adjustment::AdjustmentType::DEREF_REF:
Btype *expected_type
= TyTyResolveCompile::compile (ctx, adjustment.get_expected ());
self = ctx->get_backend ()->indirect_expression (
expected_type, self, true, /* known_valid*/
expr.get_receiver ()->get_locus ());
break;
}
}
args.push_back (self);
// normal args
for (auto &argument : expr.get_arguments ())
{
Bexpression *compiled_expr = CompileExpr::Compile (argument.get (), ctx);
args.push_back (compiled_expr);
}
auto fncontext = ctx->peek_fn ();
translated
= ctx->get_backend ()->call_expression (fncontext.fndecl, fn_expr, args,
nullptr, expr.get_locus ());
}
// rust-compile-block.h
void
CompileBlock::visit (HIR::BlockExpr &expr)
{
fncontext fnctx = ctx->peek_fn ();
Bfunction *fndecl = fnctx.fndecl;
Location start_location = expr.get_locus ();
Location end_location = expr.get_closing_locus ();
auto body_mappings = expr.get_mappings ();
Resolver::Rib *rib = nullptr;
if (!ctx->get_resolver ()->find_name_rib (body_mappings.get_nodeid (), &rib))
{
rust_fatal_error (expr.get_locus (), "failed to setup locals per block");
return;
}
std::vector<Bvariable *> locals;
bool ok = compile_locals_for_block (*rib, fndecl, locals);
rust_assert (ok);
Bblock *enclosing_scope = ctx->peek_enclosing_scope ();
Bblock *new_block
= ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
start_location, end_location);
ctx->push_block (new_block);
for (auto &s : expr.get_statements ())
{
auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
if (compiled_expr != nullptr)
{
Bstatement *compiled_stmt
= ctx->get_backend ()->expression_statement (fnctx.fndecl,
compiled_expr);
ctx->add_statement (compiled_stmt);
}
}
if (expr.has_expr ())
{
// the previous passes will ensure this is a valid return or
// a valid trailing expression
Bexpression *compiled_expr = CompileExpr::Compile (expr.expr.get (), ctx);
if (compiled_expr != nullptr)
{
if (result == nullptr)
{
Bstatement *final_stmt
= ctx->get_backend ()->expression_statement (fnctx.fndecl,
compiled_expr);
ctx->add_statement (final_stmt);
}
else
{
Bexpression *result_reference
= ctx->get_backend ()->var_expression (
result, expr.get_final_expr ()->get_locus ());
Bstatement *assignment
= ctx->get_backend ()->assignment_statement (fnctx.fndecl,
result_reference,
compiled_expr,
expr.get_locus ());
ctx->add_statement (assignment);
}
}
}
ctx->pop_block ();
translated = new_block;
}
void
CompileConditionalBlocks::visit (HIR::IfExpr &expr)
{
fncontext fnctx = ctx->peek_fn ();
Bfunction *fndecl = fnctx.fndecl;
Bexpression *condition_expr
= CompileExpr::Compile (expr.get_if_condition (), ctx);
Bblock *then_block
= CompileBlock::compile (expr.get_if_block (), ctx, result);
translated
= ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block,
NULL, expr.get_locus ());
}
void
CompileConditionalBlocks::visit (HIR::IfExprConseqElse &expr)
{
fncontext fnctx = ctx->peek_fn ();
Bfunction *fndecl = fnctx.fndecl;
Bexpression *condition_expr
= CompileExpr::Compile (expr.get_if_condition (), ctx);
Bblock *then_block
= CompileBlock::compile (expr.get_if_block (), ctx, result);
Bblock *else_block
= CompileBlock::compile (expr.get_else_block (), ctx, result);
translated
= ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block,
else_block, expr.get_locus ());
}
void
CompileConditionalBlocks::visit (HIR::IfExprConseqIf &expr)
{
fncontext fnctx = ctx->peek_fn ();
Bfunction *fndecl = fnctx.fndecl;
Bexpression *condition_expr
= CompileExpr::Compile (expr.get_if_condition (), ctx);
Bblock *then_block
= CompileBlock::compile (expr.get_if_block (), ctx, result);
// else block
std::vector<Bvariable *> locals;
Location start_location = expr.get_conseq_if_expr ()->get_locus ();
Location end_location = expr.get_conseq_if_expr ()->get_locus (); // FIXME
Bblock *enclosing_scope = ctx->peek_enclosing_scope ();
Bblock *else_block
= ctx->get_backend ()->block (fndecl, enclosing_scope, locals,
start_location, end_location);
ctx->push_block (else_block);
Bstatement *else_stmt_decl
= CompileConditionalBlocks::compile (expr.get_conseq_if_expr (), ctx,
result);
ctx->add_statement (else_stmt_decl);
ctx->pop_block ();
translated
= ctx->get_backend ()->if_statement (fndecl, condition_expr, then_block,
else_block, expr.get_locus ());
}
// rust-compile-struct-field-expr.h
void
CompileStructExprField::visit (HIR::StructExprFieldIdentifierValue &field)
{
translated = CompileExpr::Compile (field.get_value (), ctx);
}
void
CompileStructExprField::visit (HIR::StructExprFieldIndexValue &field)
{
translated = CompileExpr::Compile (field.get_value (), ctx);
}
void
CompileStructExprField::visit (HIR::StructExprFieldIdentifier &field)
{
// we can make the field look like an identifier expr to take advantage of
// existing code
HIR::IdentifierExpr expr (field.get_mappings (), field.get_field_name (),
field.get_locus ());
translated = CompileExpr::Compile (&expr, ctx);
}
// Shared methods in compilation
void
HIRCompileBase::compile_function_body (
Bfunction *fndecl, std::unique_ptr<HIR::BlockExpr> &function_body,
bool has_return_type)
{
for (auto &s : function_body->get_statements ())
{
auto compiled_expr = CompileStmt::Compile (s.get (), ctx);
if (compiled_expr != nullptr)
{
Bstatement *compiled_stmt
= ctx->get_backend ()->expression_statement (fndecl, compiled_expr);
ctx->add_statement (compiled_stmt);
}
}
if (function_body->has_expr ())
{
// the previous passes will ensure this is a valid return
// or a valid trailing expression
Bexpression *compiled_expr
= CompileExpr::Compile (function_body->expr.get (), ctx);
if (compiled_expr != nullptr)
{
if (has_return_type)
{
std::vector<Bexpression *> retstmts;
retstmts.push_back (compiled_expr);
auto ret = ctx->get_backend ()->return_statement (
fndecl, retstmts,
function_body->get_final_expr ()->get_locus ());
ctx->add_statement (ret);
}
else
{
Bstatement *final_stmt
= ctx->get_backend ()->expression_statement (fndecl,
compiled_expr);
ctx->add_statement (final_stmt);
}
}
}
}
bool
HIRCompileBase::compile_locals_for_block (Resolver::Rib &rib, Bfunction *fndecl,
std::vector<Bvariable *> &locals)
{
rib.iterate_decls ([&] (NodeId n, Location) mutable -> bool {
Resolver::Definition d;
bool ok = ctx->get_resolver ()->lookup_definition (n, &d);
rust_assert (ok);
HIR::Stmt *decl = nullptr;
ok = ctx->get_mappings ()->resolve_nodeid_to_stmt (d.parent, &decl);
rust_assert (ok);
// if its a function we extract this out side of this fn context
// and it is not a local to this function
bool is_item = ctx->get_mappings ()->lookup_hir_item (
decl->get_mappings ().get_crate_num (),
decl->get_mappings ().get_hirid ())
!= nullptr;
if (is_item)
{
HIR::Item *item = static_cast<HIR::Item *> (decl);
CompileItem::compile (item, ctx, true);
return true;
}
Bvariable *compiled = CompileVarDecl::compile (fndecl, decl, ctx);
locals.push_back (compiled);
return true;
});
return true;
}
Bexpression *
HIRCompileBase::coercion_site (Bexpression *compiled_ref,
TyTy::BaseType *actual, TyTy::BaseType *expected,
Location locus)
{
auto root_actual_kind = actual->get_root ()->get_kind ();
auto root_expected_kind = expected->get_root ()->get_kind ();
if (root_expected_kind == TyTy::TypeKind::DYNAMIC
&& root_actual_kind != TyTy::TypeKind::DYNAMIC)
{
const TyTy::DynamicObjectType *dyn
= static_cast<const TyTy::DynamicObjectType *> (expected->get_root ());
return coerce_to_dyn_object (compiled_ref, actual, expected, dyn, locus);
}
return compiled_ref;
}
Bexpression *
HIRCompileBase::coerce_to_dyn_object (Bexpression *compiled_ref,
const TyTy::BaseType *actual,
const TyTy::BaseType *expected,
const TyTy::DynamicObjectType *ty,
Location locus)
{
Btype *dynamic_object = TyTyResolveCompile::compile (ctx, ty);
//' this assumes ordering and current the structure is
// __trait_object_ptr
// [list of function ptrs]
std::vector<Bexpression *> vals;
vals.push_back (compiled_ref);
for (auto &item : ty->get_object_items ())
{
// compute the address of each method item
auto address = compute_address_for_trait_item (item, actual->get_root ());
vals.push_back (address);
}
Bexpression *constructed_trait_object
= ctx->get_backend ()->constructor_expression (dynamic_object, vals, -1,
locus);
fncontext fnctx = ctx->peek_fn ();
Bblock *enclosing_scope = ctx->peek_enclosing_scope ();
bool is_address_taken = false;
Bstatement *ret_var_stmt = nullptr;
Bvariable *dyn_tmp = ctx->get_backend ()->temporary_variable (
fnctx.fndecl, enclosing_scope, dynamic_object, constructed_trait_object,
is_address_taken, locus, &ret_var_stmt);
ctx->add_statement (ret_var_stmt);
// FIXME this needs to be more generic to apply any covariance
auto e = expected;
std::vector<Resolver::Adjustment> adjustments;
while (e->get_kind () == TyTy::TypeKind::REF)
{
auto r = static_cast<const TyTy::ReferenceType *> (e);
e = r->get_base ();
if (r->is_mutable ())
adjustments.push_back (
Resolver::Adjustment (Resolver::Adjustment::AdjustmentType::MUT_REF,
e));
else
adjustments.push_back (
Resolver::Adjustment (Resolver::Adjustment::AdjustmentType::IMM_REF,
e));
}
auto resulting_dyn_object_ref
= ctx->get_backend ()->var_expression (dyn_tmp, locus);
for (auto it = adjustments.rbegin (); it != adjustments.rend (); it++)
{
bool ok
= it->get_type () == Resolver::Adjustment::AdjustmentType::IMM_REF
|| it->get_type () == Resolver::Adjustment::AdjustmentType::MUT_REF;
rust_assert (ok);
resulting_dyn_object_ref
= ctx->get_backend ()->address_expression (resulting_dyn_object_ref,
locus);
}
return resulting_dyn_object_ref;
}
Bexpression *
HIRCompileBase::compute_address_for_trait_item (
const Resolver::TraitItemReference *trait_item_ref,
const TyTy::BaseType *receiver)
{
TyTy::BaseType *item_type = trait_item_ref->get_tyty ();
rust_assert (item_type->get_kind () == TyTy::TypeKind::FNDEF);
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (item_type);
auto root = receiver->get_root ();
HIR::PathIdentSegment segment_name (trait_item_ref->get_identifier ());
std::vector<Resolver::PathProbeCandidate> candidates
= Resolver::PathProbeType::Probe (root, segment_name, true, false, true);
// FIXME for default trait item resolution
//
// if (candidates.size () == 0)
// {
// rust_assert (trait_item_ref->is_optional ()); // has definition
//
// CompileTraitItem::Compile (self_type,
// trait_item_ref->get_hir_trait_item (), ctx,
// fntype);
// if (!ctx->lookup_function_decl (fntype->get_ty_ref (), &fn))
// {
// return ctx->get_backend ()->error_expression ();
// }
// }
rust_assert (!candidates.empty ());
rust_assert (candidates.size () == 1);
Resolver::PathProbeCandidate *candidate = &candidates.at (0);
rust_assert (candidate->is_impl_candidate ());
HIR::ImplItem *impl_item = candidate->item.impl.impl_item;
return CompileInherentImplItem::Compile (receiver->get_root (), impl_item,
ctx, true, fntype, true,
Location () /* FIXME */);
}
} // namespace Compile
} // namespace Rust
|