@@ -2998,10 +2998,7 @@ Lower_parse_tree::constant(Named_object* no, bool)
2998
2998
return TRAVERSE_CONTINUE;
2999
2999
nc->set_lowering ();
3000
3000
3001
- go_assert (this ->iota_value_ == -1 );
3002
- this ->iota_value_ = nc->iota_value ();
3003
3001
nc->traverse_expression (this );
3004
- this ->iota_value_ = -1 ;
3005
3002
3006
3003
nc->clear_lowering ();
3007
3004
@@ -3018,8 +3015,6 @@ Lower_parse_tree::constant(Named_object* no, bool)
3018
3015
int
3019
3016
Lower_parse_tree::function (Named_object* no)
3020
3017
{
3021
- no->func_value ()->set_closure_type ();
3022
-
3023
3018
go_assert (this ->function_ == NULL );
3024
3019
this ->function_ = no;
3025
3020
int t = no->func_value ()->traverse (this );
@@ -3482,6 +3477,43 @@ Gogo::create_function_descriptors()
3482
3477
this ->traverse (&cfd);
3483
3478
}
3484
3479
3480
+ // Lower calls to builtin functions. We need to do this early because
3481
+ // some builtin calls are constant expressions. In particular we need
3482
+ // to do this before finalize_methods, because finalize_methods calls
3483
+ // is_direct_iface_type, which needs to know whether something like
3484
+ // [unsafe.Sizeof(byte(0))]*byte is a direct-iface type.
3485
+
3486
+ class Lower_builtin_calls : public Traverse
3487
+ {
3488
+ public:
3489
+ Lower_builtin_calls (Gogo* gogo)
3490
+ : Traverse(traverse_expressions),
3491
+ gogo_ (gogo)
3492
+ { }
3493
+
3494
+ int
3495
+ expression (Expression**);
3496
+
3497
+ private:
3498
+ Gogo* gogo_;
3499
+ };
3500
+
3501
+ int
3502
+ Lower_builtin_calls::expression (Expression** pexpr)
3503
+ {
3504
+ Call_expression* ce = (*pexpr)->call_expression ();
3505
+ if (ce != NULL )
3506
+ *pexpr = ce->lower_builtin (this ->gogo_ );
3507
+ return TRAVERSE_CONTINUE;
3508
+ }
3509
+
3510
+ void
3511
+ Gogo::lower_builtin_calls ()
3512
+ {
3513
+ Lower_builtin_calls lbc (this );
3514
+ this ->traverse (&lbc);
3515
+ }
3516
+
3485
3517
// Finalize the methods of an interface type.
3486
3518
3487
3519
int
@@ -3625,47 +3657,7 @@ Gogo::finalize_methods_for_type(Type* type)
3625
3657
void
3626
3658
Gogo::determine_types ()
3627
3659
{
3628
- Bindings* bindings = this ->current_bindings ();
3629
- for (Bindings::const_definitions_iterator p = bindings->begin_definitions ();
3630
- p != bindings->end_definitions ();
3631
- ++p)
3632
- {
3633
- if ((*p)->is_function ())
3634
- (*p)->func_value ()->determine_types (this );
3635
- else if ((*p)->is_variable ())
3636
- (*p)->var_value ()->determine_type (this );
3637
- else if ((*p)->is_const ())
3638
- (*p)->const_value ()->determine_type (this );
3639
-
3640
- // See if a variable requires us to build an initialization
3641
- // function. We know that we will see all global variables
3642
- // here.
3643
- if (!this ->need_init_fn_ && (*p)->is_variable ())
3644
- {
3645
- Variable* variable = (*p)->var_value ();
3646
-
3647
- // If this is a global variable which requires runtime
3648
- // initialization, we need an initialization function.
3649
- if (!variable->is_global ())
3650
- ;
3651
- else if (variable->init () == NULL )
3652
- ;
3653
- else if (variable->type ()->interface_type () != NULL )
3654
- this ->need_init_fn_ = true ;
3655
- else if (variable->init ()->is_constant ())
3656
- ;
3657
- else if (!variable->init ()->is_composite_literal ())
3658
- this ->need_init_fn_ = true ;
3659
- else if (variable->init ()->is_nonconstant_composite_literal ())
3660
- this ->need_init_fn_ = true ;
3661
-
3662
- // If this is a global variable which holds a pointer value,
3663
- // then we need an initialization function to register it as a
3664
- // GC root.
3665
- if (variable->is_global () && variable->type ()->has_pointer ())
3666
- this ->need_init_fn_ = true ;
3667
- }
3668
- }
3660
+ this ->current_bindings ()->determine_types (this );
3669
3661
3670
3662
// Determine the types of constants in packages.
3671
3663
for (Packages::const_iterator p = this ->packages_ .begin ();
@@ -3756,15 +3748,23 @@ Check_types_traverse::variable(Named_object* named_object)
3756
3748
no->message_name ().c_str ());
3757
3749
}
3758
3750
}
3751
+
3759
3752
if (!var->is_used ()
3760
3753
&& !var->is_global ()
3761
3754
&& !var->is_parameter ()
3762
3755
&& !var->is_receiver ()
3763
3756
&& !var->type ()->is_error ()
3764
3757
&& (init == NULL || !init->is_error_expression ())
3765
3758
&& !Lex::is_invalid_identifier (named_object->name ()))
3766
- go_error_at (var->location (), " %qs declared but not used" ,
3767
- named_object->message_name ().c_str ());
3759
+ {
3760
+ // Avoid giving an error if the initializer is invalid.
3761
+ if (init != NULL )
3762
+ init->check_types (this ->gogo_ );
3763
+
3764
+ if (init == NULL || !init->is_error_expression ())
3765
+ go_error_at (var->location (), " %qs declared but not used" ,
3766
+ named_object->message_name ().c_str ());
3767
+ }
3768
3768
}
3769
3769
return TRAVERSE_CONTINUE;
3770
3770
}
@@ -3788,6 +3788,11 @@ Check_types_traverse::constant(Named_object* named_object, bool)
3788
3788
go_error_at (constant->location (), " invalid constant type" );
3789
3789
constant->set_error ();
3790
3790
}
3791
+ else if (constant->expr ()->is_error_expression ())
3792
+ {
3793
+ go_assert (saw_errors ());
3794
+ constant->set_error ();
3795
+ }
3791
3796
else if (!constant->expr ()->is_constant ())
3792
3797
{
3793
3798
go_error_at (constant->expr ()->location (), " expression is not constant" );
@@ -4396,6 +4401,7 @@ Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
4396
4401
4397
4402
Statement* if_statement = Statement::make_if_statement (cond, block, NULL ,
4398
4403
loc);
4404
+ if_statement->determine_types (this ->gogo_ );
4399
4405
retblock->add_statement (if_statement);
4400
4406
4401
4407
*pshortcut = Expression::make_temporary_reference (ts, loc);
@@ -4817,7 +4823,7 @@ Build_recover_thunks::function(Named_object* orig_no)
4817
4823
// Any varargs call has already been lowered.
4818
4824
call->set_varargs_are_lowered ();
4819
4825
4820
- Statement* s = Statement::make_return_from_call (call, location);
4826
+ Statement* s = Statement::make_return_from_call (new_no, call, location);
4821
4827
s->determine_types (this ->gogo_ );
4822
4828
gogo->add_statement (s);
4823
4829
@@ -5894,6 +5900,7 @@ Function::traverse(Traverse* traverse)
5894
5900
void
5895
5901
Function::determine_types (Gogo* gogo)
5896
5902
{
5903
+ this ->set_closure_type ();
5897
5904
if (this ->block_ != NULL )
5898
5905
this ->block_ ->determine_types (gogo);
5899
5906
}
@@ -7465,8 +7472,8 @@ Function_declaration::import_function_body(Gogo* gogo, Named_object* no)
7465
7472
if (!Block::import_block (outer, &ifb, start_loc))
7466
7473
return ;
7467
7474
7468
- gogo->lower_block (no, outer);
7469
7475
outer->determine_types (gogo);
7476
+ gogo->lower_block (no, outer);
7470
7477
7471
7478
gogo->add_imported_inline_function (no);
7472
7479
}
@@ -7670,9 +7677,13 @@ Variable::has_type() const
7670
7677
Type*
7671
7678
Variable::type_from_tuple (Expression* expr, bool report_error) const
7672
7679
{
7673
- if (expr-> map_index_expression () != NULL )
7680
+ if (Index_expression::is_map_index (expr) )
7674
7681
{
7675
- Map_type* mt = expr->map_index_expression ()->get_map_type ();
7682
+ Map_type* mt;
7683
+ if (expr->map_index_expression () != NULL )
7684
+ mt = expr->map_index_expression ()->get_map_type ();
7685
+ else
7686
+ mt = expr->index_expression ()->left ()->type ()->map_type ();
7676
7687
if (mt == NULL )
7677
7688
return Type::make_error_type ();
7678
7689
return mt->val_type ();
@@ -7701,7 +7712,9 @@ Variable::type_from_range(Expression* expr, bool get_index_type,
7701
7712
bool report_error) const
7702
7713
{
7703
7714
Type* t = expr->type ();
7704
- if (t->array_type () != NULL
7715
+ if (t->is_error_type ())
7716
+ return t;
7717
+ else if (t->array_type () != NULL
7705
7718
|| (t->points_to () != NULL
7706
7719
&& t->points_to ()->array_type () != NULL
7707
7720
&& !t->points_to ()->is_slice_type ()))
@@ -8211,14 +8224,50 @@ Named_constant::traverse_expression(Traverse* traverse)
8211
8224
return Expression::traverse (&this ->expr_ , traverse);
8212
8225
}
8213
8226
8227
+ // Set the iota value in a constant expression.
8228
+
8229
+ class Set_iota_value : public Traverse
8230
+ {
8231
+ public:
8232
+ Set_iota_value (int iota_value)
8233
+ : Traverse(traverse_expressions),
8234
+ iota_value_ (iota_value)
8235
+ { }
8236
+
8237
+ int
8238
+ expression (Expression**);
8239
+
8240
+ private:
8241
+ int iota_value_;
8242
+ };
8243
+
8244
+ int
8245
+ Set_iota_value::expression (Expression** pexpr)
8246
+ {
8247
+ Expression* expr = *pexpr;
8248
+ if (expr->const_expression () != NULL )
8249
+ expr->const_expression ()->set_iota_value (this ->iota_value_ );
8250
+ else if (expr->unknown_expression () != NULL )
8251
+ {
8252
+ // This case can happen for an array length that is not set in
8253
+ // the determine types pass.
8254
+ expr->unknown_expression ()->set_iota_value (this ->iota_value_ );
8255
+ }
8256
+ return TRAVERSE_CONTINUE;
8257
+ }
8258
+
8214
8259
// Determine the type of the constant.
8215
8260
8216
8261
void
8217
8262
Named_constant::determine_type (Gogo* gogo)
8218
8263
{
8264
+ if (this ->type_is_determined_ )
8265
+ return ;
8266
+ this ->type_is_determined_ = true ;
8267
+
8219
8268
if (this ->type_ != NULL )
8220
8269
{
8221
- Type_context context (this ->type_ , false );
8270
+ Type_context context (this ->type_ , this -> type_ -> is_abstract () );
8222
8271
this ->expr_ ->determine_type (gogo, &context);
8223
8272
}
8224
8273
else
@@ -8229,6 +8278,9 @@ Named_constant::determine_type(Gogo* gogo)
8229
8278
this ->type_ = this ->expr_ ->type ();
8230
8279
go_assert (this ->type_ != NULL );
8231
8280
}
8281
+
8282
+ Set_iota_value siv (this ->iota_value_ );
8283
+ this ->traverse_expression (&siv);
8232
8284
}
8233
8285
8234
8286
// Indicate that we found and reported an error for this constant.
@@ -9353,6 +9405,55 @@ Bindings::traverse(Traverse* traverse, bool is_global)
9353
9405
return TRAVERSE_CONTINUE;
9354
9406
}
9355
9407
9408
+ // Determine types for the objects.
9409
+
9410
+ void
9411
+ Bindings::determine_types (Gogo* gogo)
9412
+ {
9413
+ // We don't use an iterator because the traversal can add new
9414
+ // bindings.
9415
+ for (size_t i = 0 ; i < this ->named_objects_ .size (); ++i)
9416
+ {
9417
+ Named_object* no = this ->named_objects_ [i];
9418
+ if (no->is_function ())
9419
+ no->func_value ()->determine_types (gogo);
9420
+ else if (no->is_variable ())
9421
+ no->var_value ()->determine_type (gogo);
9422
+ else if (no->is_const ())
9423
+ no->const_value ()->determine_type (gogo);
9424
+
9425
+ // See if a variable requires us to build an initialization
9426
+ // function. We know that we will see all global variables
9427
+ // here.
9428
+ if (!gogo->need_init_fn () && no->is_variable ())
9429
+ {
9430
+ Variable* variable = no->var_value ();
9431
+
9432
+ // If this is a global variable which requires runtime
9433
+ // initialization, we need an initialization function.
9434
+
9435
+ if (!variable->is_global ())
9436
+ continue ;
9437
+
9438
+ if (variable->init () == NULL )
9439
+ ;
9440
+ else if (variable->type ()->interface_type () != NULL )
9441
+ gogo->set_need_init_fn ();
9442
+ else if (variable->init ()->is_constant ())
9443
+ ;
9444
+ else if (!variable->init ()->is_composite_literal ())
9445
+ gogo->set_need_init_fn ();
9446
+ else if (variable->init ()->is_nonconstant_composite_literal ())
9447
+ gogo->set_need_init_fn ();
9448
+
9449
+ // If this global variable holds a pointer value, we need an
9450
+ // initialization function to register it as a GC root.
9451
+ if (variable->type ()->has_pointer ())
9452
+ gogo->set_need_init_fn ();
9453
+ }
9454
+ }
9455
+ }
9456
+
9356
9457
void
9357
9458
Bindings::debug_dump ()
9358
9459
{
0 commit comments