-
Notifications
You must be signed in to change notification settings - Fork 126
/
statements.cc
6056 lines (5059 loc) · 166 KB
/
statements.cc
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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// statements.cc -- Go frontend statements.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include "go-c.h"
#include "go-diagnostics.h"
#include "types.h"
#include "expressions.h"
#include "gogo.h"
#include "runtime.h"
#include "backend.h"
#include "statements.h"
#include "ast-dump.h"
// Class Statement.
Statement::Statement(Statement_classification classification,
Location location)
: classification_(classification), location_(location)
{
}
Statement::~Statement()
{
}
// Traverse the tree. The work of walking the components is handled
// by the subclasses.
int
Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
{
if (this->classification_ == STATEMENT_ERROR)
return TRAVERSE_CONTINUE;
unsigned int traverse_mask = traverse->traverse_mask();
if ((traverse_mask & Traverse::traverse_statements) != 0)
{
int t = traverse->statement(block, pindex, this);
if (t == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
else if (t == TRAVERSE_SKIP_COMPONENTS)
return TRAVERSE_CONTINUE;
}
// No point in checking traverse_mask here--a statement may contain
// other blocks or statements, and if we got here we always want to
// walk them.
return this->do_traverse(traverse);
}
// Traverse the contents of a statement.
int
Statement::traverse_contents(Traverse* traverse)
{
return this->do_traverse(traverse);
}
// Traverse assignments.
bool
Statement::traverse_assignments(Traverse_assignments* tassign)
{
if (this->classification_ == STATEMENT_ERROR)
return false;
return this->do_traverse_assignments(tassign);
}
// Traverse an expression in a statement. This is a helper function
// for child classes.
int
Statement::traverse_expression(Traverse* traverse, Expression** expr)
{
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return Expression::traverse(expr, traverse);
}
// Traverse an expression list in a statement. This is a helper
// function for child classes.
int
Statement::traverse_expression_list(Traverse* traverse,
Expression_list* expr_list)
{
if (expr_list == NULL)
return TRAVERSE_CONTINUE;
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return expr_list->traverse(traverse);
}
// Traverse a type in a statement. This is a helper function for
// child classes.
int
Statement::traverse_type(Traverse* traverse, Type* type)
{
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return Type::traverse(type, traverse);
}
// Set type information for unnamed constants. This is really done by
// the child class.
void
Statement::determine_types()
{
this->do_determine_types();
}
// If this is a thunk statement, return it.
Thunk_statement*
Statement::thunk_statement()
{
Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
if (ret == NULL)
ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
return ret;
}
// Convert a Statement to the backend representation. This is really
// done by the child class.
Bstatement*
Statement::get_backend(Translate_context* context)
{
if (this->classification_ == STATEMENT_ERROR)
return context->backend()->error_statement();
return this->do_get_backend(context);
}
// Dump AST representation for a statement to a dump context.
void
Statement::dump_statement(Ast_dump_context* ast_dump_context) const
{
this->do_dump_statement(ast_dump_context);
}
// Note that this statement is erroneous. This is called by children
// when they discover an error.
void
Statement::set_is_error()
{
this->classification_ = STATEMENT_ERROR;
}
// For children to call to report an error conveniently.
void
Statement::report_error(const char* msg)
{
go_error_at(this->location_, "%s", msg);
this->set_is_error();
}
// An error statement, used to avoid crashing after we report an
// error.
class Error_statement : public Statement
{
public:
Error_statement(Location location)
: Statement(STATEMENT_ERROR, location)
{ }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
};
//
// Helper to tack on available source position information
// at the end of a statement.
//
static std::string
dsuffix(Location location)
{
std::string lstr = Linemap::location_to_string(location);
if (lstr == "")
return lstr;
std::string rval(" // ");
rval += lstr;
return rval;
}
// Dump the AST representation for an error statement.
void
Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "Error statement" << std::endl;
}
// Make an error statement.
Statement*
Statement::make_error_statement(Location location)
{
return new Error_statement(location);
}
// Class Variable_declaration_statement.
Variable_declaration_statement::Variable_declaration_statement(
Named_object* var)
: Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
var_(var)
{
}
// We don't actually traverse the variable here; it was traversed
// while traversing the Block.
int
Variable_declaration_statement::do_traverse(Traverse*)
{
return TRAVERSE_CONTINUE;
}
// Traverse the assignments in a variable declaration. Note that this
// traversal is different from the usual traversal.
bool
Variable_declaration_statement::do_traverse_assignments(
Traverse_assignments* tassign)
{
tassign->initialize_variable(this->var_);
return true;
}
// Lower the variable's initialization expression.
Statement*
Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
Block*, Statement_inserter* inserter)
{
this->var_->var_value()->lower_init_expression(gogo, function, inserter);
return this;
}
// Flatten the variable's initialization expression.
Statement*
Variable_declaration_statement::do_flatten(Gogo* gogo, Named_object* function,
Block*, Statement_inserter* inserter)
{
Variable* var = this->var_->var_value();
if (var->type()->is_error_type()
|| (var->init() != NULL
&& var->init()->is_error_expression()))
{
go_assert(saw_errors());
return Statement::make_error_statement(this->location());
}
this->var_->var_value()->flatten_init_expression(gogo, function, inserter);
return this;
}
// Convert a variable declaration to the backend representation.
Bstatement*
Variable_declaration_statement::do_get_backend(Translate_context* context)
{
Bfunction* bfunction = context->function()->func_value()->get_decl();
Variable* var = this->var_->var_value();
Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
context->function());
Bexpression* binit = var->get_init(context->gogo(), context->function());
if (!var->is_in_heap())
{
go_assert(binit != NULL);
return context->backend()->init_statement(bfunction, bvar, binit);
}
// Something takes the address of this variable, so the value is
// stored in the heap. Initialize it to newly allocated memory
// space, and assign the initial value to the new space.
Location loc = this->location();
Named_object* newfn = context->gogo()->lookup_global("new");
go_assert(newfn != NULL && newfn->is_function_declaration());
Expression* func = Expression::make_func_reference(newfn, NULL, loc);
Expression_list* params = new Expression_list();
params->push_back(Expression::make_type(var->type(), loc));
Expression* call = Expression::make_call(func, params, false, loc);
context->gogo()->lower_expression(context->function(), NULL, &call);
Temporary_statement* temp = Statement::make_temporary(NULL, call, loc);
Bstatement* btemp = temp->get_backend(context);
Bstatement* set = NULL;
if (binit != NULL)
{
Expression* e = Expression::make_temporary_reference(temp, loc);
e = Expression::make_unary(OPERATOR_MULT, e, loc);
Bexpression* be = e->get_backend(context);
set = context->backend()->assignment_statement(bfunction, be, binit, loc);
}
Expression* ref = Expression::make_temporary_reference(temp, loc);
Bexpression* bref = ref->get_backend(context);
Bstatement* sinit = context->backend()->init_statement(bfunction, bvar, bref);
std::vector<Bstatement*> stats;
stats.reserve(3);
stats.push_back(btemp);
if (set != NULL)
stats.push_back(set);
stats.push_back(sinit);
return context->backend()->statement_list(stats);
}
// Dump the AST representation for a variable declaration.
void
Variable_declaration_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
go_assert(var_->is_variable());
ast_dump_context->ostream() << "var " << this->var_->name() << " ";
Variable* var = this->var_->var_value();
if (var->has_type())
{
ast_dump_context->dump_type(var->type());
ast_dump_context->ostream() << " ";
}
if (var->init() != NULL)
{
ast_dump_context->ostream() << "= ";
ast_dump_context->dump_expression(var->init());
}
ast_dump_context->ostream() << dsuffix(location()) << std::endl;
}
// Make a variable declaration.
Statement*
Statement::make_variable_declaration(Named_object* var)
{
return new Variable_declaration_statement(var);
}
// Class Temporary_statement.
// Return the type of the temporary variable.
Type*
Temporary_statement::type() const
{
Type* type = this->type_ != NULL ? this->type_ : this->init_->type();
// Temporary variables cannot have a void type.
if (type->is_void_type())
{
go_assert(saw_errors());
return Type::make_error_type();
}
return type;
}
// Traversal.
int
Temporary_statement::do_traverse(Traverse* traverse)
{
if (this->type_ != NULL
&& this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
if (this->init_ == NULL)
return TRAVERSE_CONTINUE;
else
return this->traverse_expression(traverse, &this->init_);
}
// Traverse assignments.
bool
Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
if (this->init_ == NULL)
return false;
tassign->value(&this->init_, true, true);
return true;
}
// Determine types.
void
Temporary_statement::do_determine_types()
{
if (this->type_ != NULL && this->type_->is_abstract())
this->type_ = this->type_->make_non_abstract_type();
if (this->init_ != NULL)
{
if (this->type_ == NULL)
this->init_->determine_type_no_context();
else
{
Type_context context(this->type_, false);
this->init_->determine_type(&context);
}
}
if (this->type_ == NULL)
{
this->type_ = this->init_->type();
go_assert(!this->type_->is_abstract());
}
}
// Check types.
void
Temporary_statement::do_check_types(Gogo*)
{
if (this->type_ != NULL && this->init_ != NULL)
{
std::string reason;
if (!Type::are_assignable(this->type_, this->init_->type(), &reason))
{
if (reason.empty())
go_error_at(this->location(), "incompatible types in assignment");
else
go_error_at(this->location(), "incompatible types in assignment (%s)",
reason.c_str());
this->set_is_error();
}
}
}
// Flatten a temporary statement: add another temporary when it might
// be needed for interface conversion.
Statement*
Temporary_statement::do_flatten(Gogo*, Named_object*, Block*,
Statement_inserter* inserter)
{
if (this->type()->is_error_type()
|| (this->init_ != NULL
&& this->init_->is_error_expression()))
{
go_assert(saw_errors());
return Statement::make_error_statement(this->location());
}
if (this->type_ != NULL
&& this->init_ != NULL
&& !Type::are_identical(this->type_, this->init_->type(), false, NULL)
&& this->init_->type()->interface_type() != NULL
&& !this->init_->is_variable())
{
Temporary_statement *temp =
Statement::make_temporary(NULL, this->init_, this->location());
inserter->insert(temp);
this->init_ = Expression::make_temporary_reference(temp,
this->location());
}
return this;
}
// Convert to backend representation.
Bstatement*
Temporary_statement::do_get_backend(Translate_context* context)
{
go_assert(this->bvariable_ == NULL);
Named_object* function = context->function();
go_assert(function != NULL);
Bfunction* bfunction = function->func_value()->get_decl();
Btype* btype = this->type()->get_backend(context->gogo());
Bexpression* binit;
if (this->init_ == NULL)
binit = NULL;
else if (this->type_ == NULL)
binit = this->init_->get_backend(context);
else
{
Expression* init = Expression::convert_for_assignment(context->gogo(),
this->type_,
this->init_,
this->location());
binit = init->get_backend(context);
}
if (binit != NULL)
binit = context->backend()->convert_expression(btype, binit,
this->location());
Bstatement* statement;
this->bvariable_ =
context->backend()->temporary_variable(bfunction, context->bblock(),
btype, binit,
this->is_address_taken_,
this->location(), &statement);
return statement;
}
// Return the backend variable.
Bvariable*
Temporary_statement::get_backend_variable(Translate_context* context) const
{
if (this->bvariable_ == NULL)
{
go_assert(saw_errors());
return context->backend()->error_variable();
}
return this->bvariable_;
}
// Dump the AST represemtation for a temporary statement
void
Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_temp_variable_name(this);
if (this->type_ != NULL)
{
ast_dump_context->ostream() << " ";
ast_dump_context->dump_type(this->type_);
}
if (this->init_ != NULL)
{
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression(this->init_);
}
ast_dump_context->ostream() << dsuffix(location()) << std::endl;
}
// Make and initialize a temporary variable in BLOCK.
Temporary_statement*
Statement::make_temporary(Type* type, Expression* init,
Location location)
{
return new Temporary_statement(type, init, location);
}
// The Move_subexpressions class is used to move all top-level
// subexpressions of an expression. This is used for things like
// index expressions in which we must evaluate the index value before
// it can be changed by a multiple assignment.
class Move_subexpressions : public Traverse
{
public:
Move_subexpressions(int skip, Block* block)
: Traverse(traverse_expressions),
skip_(skip), block_(block)
{ }
protected:
int
expression(Expression**);
private:
// The number of subexpressions to skip moving. This is used to
// avoid moving the array itself, as we only need to move the index.
int skip_;
// The block where new temporary variables should be added.
Block* block_;
};
int
Move_subexpressions::expression(Expression** pexpr)
{
if (this->skip_ > 0)
--this->skip_;
else if ((*pexpr)->temporary_reference_expression() == NULL
&& !(*pexpr)->is_nil_expression()
&& !(*pexpr)->is_constant())
{
Location loc = (*pexpr)->location();
Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
this->block_->add_statement(temp);
*pexpr = Expression::make_temporary_reference(temp, loc);
}
// We only need to move top-level subexpressions.
return TRAVERSE_SKIP_COMPONENTS;
}
// The Move_ordered_evals class is used to find any subexpressions of
// an expression that have an evaluation order dependency. It creates
// temporary variables to hold them.
class Move_ordered_evals : public Traverse
{
public:
Move_ordered_evals(Block* block)
: Traverse(traverse_expressions),
block_(block)
{ }
protected:
int
expression(Expression**);
private:
// The block where new temporary variables should be added.
Block* block_;
};
int
Move_ordered_evals::expression(Expression** pexpr)
{
// We have to look at subexpressions first.
if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
int i;
if ((*pexpr)->must_eval_subexpressions_in_order(&i))
{
Move_subexpressions ms(i, this->block_);
if ((*pexpr)->traverse_subexpressions(&ms) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
if ((*pexpr)->must_eval_in_order())
{
Call_expression* call = (*pexpr)->call_expression();
if (call != NULL && call->is_multi_value_arg())
{
// A call expression which returns multiple results as an argument
// to another call must be handled specially. We can't create a
// temporary because there is no type to give it. Instead, group
// the caller and this multi-valued call argument and use a temporary
// variable to hold them.
return TRAVERSE_SKIP_COMPONENTS;
}
Location loc = (*pexpr)->location();
Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
this->block_->add_statement(temp);
*pexpr = Expression::make_temporary_reference(temp, loc);
}
return TRAVERSE_SKIP_COMPONENTS;
}
// Class Assignment_statement.
// Traversal.
int
Assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->rhs_);
}
bool
Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
tassign->assignment(&this->lhs_, &this->rhs_);
return true;
}
// Lower an assignment to a map index expression to a runtime function
// call.
Statement*
Assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
Statement_inserter*)
{
Map_index_expression* mie = this->lhs_->map_index_expression();
if (mie != NULL)
{
Location loc = this->location();
Expression* map = mie->map();
Map_type* mt = map->type()->map_type();
if (mt == NULL)
{
go_assert(saw_errors());
return Statement::make_error_statement(loc);
}
Block* b = new Block(enclosing, loc);
// Move out any subexpressions on the left hand side to make
// sure that functions are called in the required order.
Move_ordered_evals moe(b);
mie->traverse_subexpressions(&moe);
// Copy the key into a temporary so that we can take its address
// without pushing the value onto the heap.
// var key_temp KEY_TYPE = MAP_INDEX
Temporary_statement* key_temp = Statement::make_temporary(mt->key_type(),
mie->index(),
loc);
b->add_statement(key_temp);
// Copy the value into a temporary to ensure that it is
// evaluated before we add the key to the map. This may matter
// if the value is itself a reference to the map.
// var val_temp VAL_TYPE = RHS
Temporary_statement* val_temp = Statement::make_temporary(mt->val_type(),
this->rhs_,
loc);
b->add_statement(val_temp);
// *mapassign(TYPE, MAP, &key_temp) = RHS
Expression* a1 = Expression::make_type_descriptor(mt, loc);
Expression* a2 = mie->map();
Temporary_reference_expression* ref =
Expression::make_temporary_reference(key_temp, loc);
Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
Expression* call = Runtime::make_call(Runtime::MAPASSIGN, loc, 3,
a1, a2, a3);
Type* ptrval_type = Type::make_pointer_type(mt->val_type());
call = Expression::make_cast(ptrval_type, call, loc);
Expression* indir = Expression::make_unary(OPERATOR_MULT, call, loc);
ref = Expression::make_temporary_reference(val_temp, loc);
b->add_statement(Statement::make_assignment(indir, ref, loc));
return Statement::make_block_statement(b, loc);
}
return this;
}
// Set types for the assignment.
void
Assignment_statement::do_determine_types()
{
this->lhs_->determine_type_no_context();
Type* rhs_context_type = this->lhs_->type();
if (rhs_context_type->is_sink_type())
rhs_context_type = NULL;
Type_context context(rhs_context_type, false);
this->rhs_->determine_type(&context);
}
// Check types for an assignment.
void
Assignment_statement::do_check_types(Gogo*)
{
// The left hand side must be either addressable, a map index
// expression, or the blank identifier.
if (!this->lhs_->is_addressable()
&& this->lhs_->map_index_expression() == NULL
&& !this->lhs_->is_sink_expression())
{
if (!this->lhs_->type()->is_error())
this->report_error(_("invalid left hand side of assignment"));
return;
}
Type* lhs_type = this->lhs_->type();
Type* rhs_type = this->rhs_->type();
// Invalid assignment of nil to the blank identifier.
if (lhs_type->is_sink_type()
&& rhs_type->is_nil_type())
{
this->report_error(_("use of untyped nil"));
return;
}
std::string reason;
if (!Type::are_assignable(lhs_type, rhs_type, &reason))
{
if (reason.empty())
go_error_at(this->location(), "incompatible types in assignment");
else
go_error_at(this->location(), "incompatible types in assignment (%s)",
reason.c_str());
this->set_is_error();
}
if (lhs_type->is_error() || rhs_type->is_error())
this->set_is_error();
}
// Flatten an assignment statement. We may need a temporary for
// interface conversion.
Statement*
Assignment_statement::do_flatten(Gogo*, Named_object*, Block*,
Statement_inserter* inserter)
{
if (this->lhs_->is_error_expression()
|| this->lhs_->type()->is_error_type()
|| this->rhs_->is_error_expression()
|| this->rhs_->type()->is_error_type())
{
go_assert(saw_errors());
return Statement::make_error_statement(this->location());
}
if (!this->lhs_->is_sink_expression()
&& !Type::are_identical(this->lhs_->type(), this->rhs_->type(),
false, NULL)
&& this->rhs_->type()->interface_type() != NULL
&& !this->rhs_->is_variable())
{
Temporary_statement* temp =
Statement::make_temporary(NULL, this->rhs_, this->location());
inserter->insert(temp);
this->rhs_ = Expression::make_temporary_reference(temp,
this->location());
}
return this;
}
// Helper class to locate a root Var_expression within an expression
// tree and mark it as being in an "lvalue" or assignment
// context. Examples:
//
// x, y = 40, foo(w)
// x[2] = bar(v)
// x.z.w[blah(v + u)], y.another = 2, 3
//
// In the code above, vars "x" and "y" appear in lvalue / assignment
// context, whereas the other vars "v", "u", etc are in rvalue context.
//
// Note: at the moment the Var_expression version of "do_copy()"
// defaults to returning the original object, not a new object,
// meaning that a given Var_expression can be referenced from more
// than one place in the tree. This means that when we want to mark a
// Var_expression as having lvalue semantics, we need to make a copy
// of it. Example:
//
// mystruct.myfield += 42
//
// When this is lowered to eliminate the += operator, we get a tree
//
// mystruct.myfield = mystruct.field + 42
//
// in which the "mystruct" same Var_expression is referenced on both
// LHS and RHS subtrees. This in turn means that if we try to mark the
// LHS Var_expression the RHS Var_expression will also be marked. To
// address this issue, the code below clones any var_expression before
// applying an lvalue marking.
//
class Mark_lvalue_varexprs : public Traverse
{
public:
Mark_lvalue_varexprs()
: Traverse(traverse_expressions)
{ }
protected:
int
expression(Expression**);
private:
};
int Mark_lvalue_varexprs::expression(Expression** ppexpr)
{
Expression* e = *ppexpr;
Var_expression* ve = e->var_expression();
if (ve)
{
ve = new Var_expression(ve->named_object(), ve->location());
ve->set_in_lvalue_pos();
*ppexpr = ve;
return TRAVERSE_EXIT;
}
Field_reference_expression* fre = e->field_reference_expression();
if (fre != NULL)
return TRAVERSE_CONTINUE;
Array_index_expression* aie = e->array_index_expression();
if (aie != NULL)
{
Mark_lvalue_varexprs mlve;
aie->array()->traverse_subexpressions(&mlve);
return TRAVERSE_EXIT;
}
Unary_expression* ue = e->unary_expression();
if (ue && ue->op() == OPERATOR_MULT)
return TRAVERSE_CONTINUE;
return TRAVERSE_EXIT;
}
// Convert an assignment statement to the backend representation.
Bstatement*
Assignment_statement::do_get_backend(Translate_context* context)
{
if (this->lhs_->is_sink_expression())
{
Bexpression* rhs = this->rhs_->get_backend(context);
Bfunction* bfunction = context->function()->func_value()->get_decl();
return context->backend()->expression_statement(bfunction, rhs);
}
Mark_lvalue_varexprs mlve;
Expression::traverse(&this->lhs_, &mlve);
Bexpression* lhs = this->lhs_->get_backend(context);
Expression* conv =
Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),
this->rhs_, this->location());
Bexpression* rhs = conv->get_backend(context);
Bfunction* bfunction = context->function()->func_value()->get_decl();
return context->backend()->assignment_statement(bfunction, lhs, rhs,
this->location());
}
// Dump the AST representation for an assignment statement.
void
Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->lhs_);
ast_dump_context->ostream() << " = " ;
ast_dump_context->dump_expression(this->rhs_);
ast_dump_context->ostream() << dsuffix(location()) << std::endl;
}
// Make an assignment statement.
Statement*
Statement::make_assignment(Expression* lhs, Expression* rhs,
Location location)
{
return new Assignment_statement(lhs, rhs, location);
}
// An assignment operation statement.
class Assignment_operation_statement : public Statement
{
public:
Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
Location location)
: Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
op_(op), lhs_(lhs), rhs_(rhs)
{ }
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The operator (OPERATOR_PLUSEQ, etc.).
Operator op_;
// Left hand side.
Expression* lhs_;
// Right hand side.
Expression* rhs_;
};