1 // gogo-tree.cc -- convert Go frontend Gogo IR to gcc trees.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
11 #ifndef ENABLE_BUILD_WITH_CXX
19 #include "tree-iterator.h"
21 #include "langhooks.h"
24 #include "diagnostic.h"
26 #ifndef ENABLE_BUILD_WITH_CXX
32 #include "expressions.h"
33 #include "statements.h"
36 // Whether we have seen any errors.
41 return errorcount
!= 0 || sorrycount
!= 0;
47 get_identifier_from_string(const std::string
& str
)
49 return get_identifier_with_length(str
.data(), str
.length());
54 static std::map
<std::string
, tree
> builtin_functions
;
56 // Define a builtin function. BCODE is the builtin function code
57 // defined by builtins.def. NAME is the name of the builtin function.
58 // LIBNAME is the name of the corresponding library function, and is
59 // NULL if there isn't one. FNTYPE is the type of the function.
60 // CONST_P is true if the function has the const attribute.
63 define_builtin(built_in_function bcode
, const char* name
, const char* libname
,
64 tree fntype
, bool const_p
)
66 tree decl
= add_builtin_function(name
, fntype
, bcode
, BUILT_IN_NORMAL
,
69 TREE_READONLY(decl
) = 1;
70 built_in_decls
[bcode
] = decl
;
71 implicit_built_in_decls
[bcode
] = decl
;
72 builtin_functions
[name
] = decl
;
75 decl
= add_builtin_function(libname
, fntype
, bcode
, BUILT_IN_NORMAL
,
78 TREE_READONLY(decl
) = 1;
79 builtin_functions
[libname
] = decl
;
83 // Create trees for implicit builtin functions.
86 Gogo::define_builtin_function_trees()
88 /* We need to define the fetch_and_add functions, since we use them
90 tree t
= go_type_for_size(BITS_PER_UNIT
, 1);
91 tree p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
92 define_builtin(BUILT_IN_ADD_AND_FETCH_1
, "__sync_fetch_and_add_1", NULL
,
93 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
95 t
= go_type_for_size(BITS_PER_UNIT
* 2, 1);
96 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
97 define_builtin (BUILT_IN_ADD_AND_FETCH_2
, "__sync_fetch_and_add_2", NULL
,
98 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
100 t
= go_type_for_size(BITS_PER_UNIT
* 4, 1);
101 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
102 define_builtin(BUILT_IN_ADD_AND_FETCH_4
, "__sync_fetch_and_add_4", NULL
,
103 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
105 t
= go_type_for_size(BITS_PER_UNIT
* 8, 1);
106 p
= build_pointer_type(build_qualified_type(t
, TYPE_QUAL_VOLATILE
));
107 define_builtin(BUILT_IN_ADD_AND_FETCH_8
, "__sync_fetch_and_add_8", NULL
,
108 build_function_type_list(t
, p
, t
, NULL_TREE
), false);
110 // We use __builtin_expect for magic import functions.
111 define_builtin(BUILT_IN_EXPECT
, "__builtin_expect", NULL
,
112 build_function_type_list(long_integer_type_node
,
113 long_integer_type_node
,
114 long_integer_type_node
,
118 // We use __builtin_memmove for the predeclared copy function.
119 define_builtin(BUILT_IN_MEMMOVE
, "__builtin_memmove", "memmove",
120 build_function_type_list(ptr_type_node
,
127 // We provide sqrt for the math library.
128 define_builtin(BUILT_IN_SQRT
, "__builtin_sqrt", "sqrt",
129 build_function_type_list(double_type_node
,
133 define_builtin(BUILT_IN_SQRTL
, "__builtin_sqrtl", "sqrtl",
134 build_function_type_list(long_double_type_node
,
135 long_double_type_node
,
139 // We use __builtin_return_address in the thunk we build for
140 // functions which call recover.
141 define_builtin(BUILT_IN_RETURN_ADDRESS
, "__builtin_return_address", NULL
,
142 build_function_type_list(ptr_type_node
,
147 // The compiler uses __builtin_trap for some exception handling
149 define_builtin(BUILT_IN_TRAP
, "__builtin_trap", NULL
,
150 build_function_type(void_type_node
, void_list_node
),
154 // Get the name to use for the import control function. If there is a
155 // global function or variable, then we know that that name must be
156 // unique in the link, and we use it as the basis for our name.
159 Gogo::get_init_fn_name()
161 if (this->init_fn_name_
.empty())
163 gcc_assert(this->package_
!= NULL
);
164 if (this->is_main_package())
166 // Use a name which the runtime knows.
167 this->init_fn_name_
= "__go_init_main";
171 std::string s
= this->unique_prefix();
173 s
.append(this->package_name());
174 s
.append("..import");
175 this->init_fn_name_
= s
;
179 return this->init_fn_name_
;
182 // Add statements to INIT_STMT_LIST which run the initialization
183 // functions for imported packages. This is only used for the "main"
187 Gogo::init_imports(tree
* init_stmt_list
)
189 gcc_assert(this->is_main_package());
191 if (this->imported_init_fns_
.empty())
194 tree fntype
= build_function_type(void_type_node
, void_list_node
);
196 // We must call them in increasing priority order.
197 std::vector
<Import_init
> v
;
198 for (std::set
<Import_init
>::const_iterator p
=
199 this->imported_init_fns_
.begin();
200 p
!= this->imported_init_fns_
.end();
203 std::sort(v
.begin(), v
.end());
205 for (std::vector
<Import_init
>::const_iterator p
= v
.begin();
209 std::string user_name
= p
->package_name() + ".init";
210 tree decl
= build_decl(UNKNOWN_LOCATION
, FUNCTION_DECL
,
211 get_identifier_from_string(user_name
),
213 const std::string
& init_name(p
->init_name());
214 SET_DECL_ASSEMBLER_NAME(decl
, get_identifier_from_string(init_name
));
215 TREE_PUBLIC(decl
) = 1;
216 DECL_EXTERNAL(decl
) = 1;
217 append_to_statement_list(build_call_expr(decl
, 0), init_stmt_list
);
221 // Register global variables with the garbage collector. We need to
222 // register all variables which can hold a pointer value. They become
223 // roots during the mark phase. We build a struct that is easy to
224 // hook into a list of roots.
226 // struct __go_gc_root_list
228 // struct __go_gc_root_list* __next;
229 // struct __go_gc_root
236 // The last entry in the roots array has a NULL decl field.
239 Gogo::register_gc_vars(const std::vector
<Named_object
*>& var_gc
,
240 tree
* init_stmt_list
)
245 size_t count
= var_gc
.size();
247 tree root_type
= Gogo::builtin_struct(NULL
, "__go_gc_root", NULL_TREE
, 2,
253 tree index_type
= build_index_type(size_int(count
));
254 tree array_type
= build_array_type(root_type
, index_type
);
256 tree root_list_type
= make_node(RECORD_TYPE
);
257 root_list_type
= Gogo::builtin_struct(NULL
, "__go_gc_root_list",
260 build_pointer_type(root_list_type
),
264 // Build an initialier for the __roots array.
266 VEC(constructor_elt
,gc
)* roots_init
= VEC_alloc(constructor_elt
, gc
,
270 for (std::vector
<Named_object
*>::const_iterator p
= var_gc
.begin();
274 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
276 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
277 tree field
= TYPE_FIELDS(root_type
);
279 tree decl
= (*p
)->get_tree(this, NULL
);
280 gcc_assert(TREE_CODE(decl
) == VAR_DECL
);
281 elt
->value
= build_fold_addr_expr(decl
);
283 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
284 field
= DECL_CHAIN(field
);
286 elt
->value
= DECL_SIZE_UNIT(decl
);
288 elt
= VEC_quick_push(constructor_elt
, roots_init
, NULL
);
289 elt
->index
= size_int(i
);
290 elt
->value
= build_constructor(root_type
, init
);
293 // The list ends with a NULL entry.
295 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
297 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
298 tree field
= TYPE_FIELDS(root_type
);
300 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
302 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
303 field
= DECL_CHAIN(field
);
305 elt
->value
= size_zero_node
;
307 elt
= VEC_quick_push(constructor_elt
, roots_init
, NULL
);
308 elt
->index
= size_int(i
);
309 elt
->value
= build_constructor(root_type
, init
);
311 // Build a constructor for the struct.
313 VEC(constructor_elt
,gc
*) root_list_init
= VEC_alloc(constructor_elt
, gc
, 2);
315 elt
= VEC_quick_push(constructor_elt
, root_list_init
, NULL
);
316 field
= TYPE_FIELDS(root_list_type
);
318 elt
->value
= fold_convert(TREE_TYPE(field
), null_pointer_node
);
320 elt
= VEC_quick_push(constructor_elt
, root_list_init
, NULL
);
321 field
= DECL_CHAIN(field
);
323 elt
->value
= build_constructor(array_type
, roots_init
);
325 // Build a decl to register.
327 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
,
328 create_tmp_var_name("gc"), root_list_type
);
329 DECL_EXTERNAL(decl
) = 0;
330 TREE_PUBLIC(decl
) = 0;
331 TREE_STATIC(decl
) = 1;
332 DECL_ARTIFICIAL(decl
) = 1;
333 DECL_INITIAL(decl
) = build_constructor(root_list_type
, root_list_init
);
334 rest_of_decl_compilation(decl
, 1, 0);
336 static tree register_gc_fndecl
;
337 tree call
= Gogo::call_builtin(®ister_gc_fndecl
, BUILTINS_LOCATION
,
338 "__go_register_gc_roots",
341 build_pointer_type(root_list_type
),
342 build_fold_addr_expr(decl
));
343 if (call
!= error_mark_node
)
344 append_to_statement_list(call
, init_stmt_list
);
347 // Build the decl for the initialization function.
350 Gogo::initialization_function_decl()
352 // The tedious details of building your own function. There doesn't
353 // seem to be a helper function for this.
354 std::string name
= this->package_name() + ".init";
355 tree fndecl
= build_decl(BUILTINS_LOCATION
, FUNCTION_DECL
,
356 get_identifier_from_string(name
),
357 build_function_type(void_type_node
,
359 const std::string
& asm_name(this->get_init_fn_name());
360 SET_DECL_ASSEMBLER_NAME(fndecl
, get_identifier_from_string(asm_name
));
362 tree resdecl
= build_decl(BUILTINS_LOCATION
, RESULT_DECL
, NULL_TREE
,
364 DECL_ARTIFICIAL(resdecl
) = 1;
365 DECL_CONTEXT(resdecl
) = fndecl
;
366 DECL_RESULT(fndecl
) = resdecl
;
368 TREE_STATIC(fndecl
) = 1;
369 TREE_USED(fndecl
) = 1;
370 DECL_ARTIFICIAL(fndecl
) = 1;
371 TREE_PUBLIC(fndecl
) = 1;
373 DECL_INITIAL(fndecl
) = make_node(BLOCK
);
374 TREE_USED(DECL_INITIAL(fndecl
)) = 1;
379 // Create the magic initialization function. INIT_STMT_LIST is the
380 // code that it needs to run.
383 Gogo::write_initialization_function(tree fndecl
, tree init_stmt_list
)
385 // Make sure that we thought we needed an initialization function,
386 // as otherwise we will not have reported it in the export data.
387 gcc_assert(this->is_main_package() || this->need_init_fn_
);
389 if (fndecl
== NULL_TREE
)
390 fndecl
= this->initialization_function_decl();
392 DECL_SAVED_TREE(fndecl
) = init_stmt_list
;
394 current_function_decl
= fndecl
;
395 if (DECL_STRUCT_FUNCTION(fndecl
) == NULL
)
396 push_struct_function(fndecl
);
398 push_cfun(DECL_STRUCT_FUNCTION(fndecl
));
399 cfun
->function_end_locus
= BUILTINS_LOCATION
;
401 gimplify_function_tree(fndecl
);
403 cgraph_add_new_function(fndecl
, false);
404 cgraph_mark_needed_node(cgraph_node(fndecl
));
406 current_function_decl
= NULL_TREE
;
410 // Search for references to VAR in any statements or called functions.
412 class Find_var
: public Traverse
415 // A hash table we use to avoid looping. The index is the name of a
416 // named object. We only look through objects defined in this
418 typedef Unordered_set(std::string
) Seen_objects
;
420 Find_var(Named_object
* var
, Seen_objects
* seen_objects
)
421 : Traverse(traverse_expressions
),
422 var_(var
), seen_objects_(seen_objects
), found_(false)
425 // Whether the variable was found.
428 { return this->found_
; }
431 expression(Expression
**);
434 // The variable we are looking for.
436 // Names of objects we have already seen.
437 Seen_objects
* seen_objects_
;
438 // True if the variable was found.
442 // See if EXPR refers to VAR, looking through function calls and
443 // variable initializations.
446 Find_var::expression(Expression
** pexpr
)
448 Expression
* e
= *pexpr
;
450 Var_expression
* ve
= e
->var_expression();
453 Named_object
* v
= ve
->named_object();
457 return TRAVERSE_EXIT
;
460 if (v
->is_variable() && v
->package() == NULL
)
462 Expression
* init
= v
->var_value()->init();
465 std::pair
<Seen_objects::iterator
, bool> ins
=
466 this->seen_objects_
->insert(v
->name());
469 // This is the first time we have seen this name.
470 if (Expression::traverse(&init
, this) == TRAVERSE_EXIT
)
471 return TRAVERSE_EXIT
;
477 // We traverse the code of any function we see. Note that this
478 // means that we will traverse the code of a function whose address
479 // is taken even if it is not called.
480 Func_expression
* fe
= e
->func_expression();
483 const Named_object
* f
= fe
->named_object();
484 if (f
->is_function() && f
->package() == NULL
)
486 std::pair
<Seen_objects::iterator
, bool> ins
=
487 this->seen_objects_
->insert(f
->name());
490 // This is the first time we have seen this name.
491 if (f
->func_value()->block()->traverse(this) == TRAVERSE_EXIT
)
492 return TRAVERSE_EXIT
;
497 return TRAVERSE_CONTINUE
;
500 // Return true if EXPR refers to VAR.
503 expression_requires(Expression
* expr
, Block
* preinit
, Named_object
* var
)
505 Find_var::Seen_objects seen_objects
;
506 Find_var
find_var(var
, &seen_objects
);
508 Expression::traverse(&expr
, &find_var
);
510 preinit
->traverse(&find_var
);
512 return find_var
.found();
515 // Sort variable initializations. If the initialization expression
516 // for variable A refers directly or indirectly to the initialization
517 // expression for variable B, then we must initialize B before A.
523 : var_(NULL
), init_(NULL_TREE
), waiting_(0)
526 Var_init(Named_object
* var
, tree init
)
527 : var_(var
), init_(init
), waiting_(0)
530 // Return the variable.
533 { return this->var_
; }
535 // Return the initialization expression.
538 { return this->init_
; }
540 // Return the number of variables waiting for this one to be
544 { return this->waiting_
; }
546 // Increment the number waiting.
549 { ++this->waiting_
; }
552 // The variable being initialized.
554 // The initialization expression to run.
556 // The number of variables which are waiting for this one.
560 typedef std::list
<Var_init
> Var_inits
;
562 // Sort the variable initializations. The rule we follow is that we
563 // emit them in the order they appear in the array, except that if the
564 // initialization expression for a variable V1 depends upon another
565 // variable V2 then we initialize V1 after V2.
568 sort_var_inits(Var_inits
* var_inits
)
571 while (!var_inits
->empty())
573 Var_inits::iterator p1
= var_inits
->begin();
574 Named_object
* var
= p1
->var();
575 Expression
* init
= var
->var_value()->init();
576 Block
* preinit
= var
->var_value()->preinit();
578 // Start walking through the list to see which variables VAR
579 // needs to wait for. We can skip P1->WAITING variables--that
580 // is the number we've already checked.
581 Var_inits::iterator p2
= p1
;
583 for (size_t i
= p1
->waiting(); i
> 0; --i
)
586 for (; p2
!= var_inits
->end(); ++p2
)
588 if (expression_requires(init
, preinit
, p2
->var()))
591 if (expression_requires(p2
->var()->var_value()->init(),
592 p2
->var()->var_value()->preinit(),
595 error_at(var
->location(),
596 ("initialization expressions for %qs and "
597 "%qs depend upon each other"),
598 var
->message_name().c_str(),
599 p2
->var()->message_name().c_str());
600 inform(p2
->var()->location(), "%qs defined here",
601 p2
->var()->message_name().c_str());
602 p2
= var_inits
->end();
606 // We can't emit P1 until P2 is emitted. Move P1.
607 // Note that the WAITING loop always executes at
608 // least once, which is what we want.
609 p2
->increment_waiting();
610 Var_inits::iterator p3
= p2
;
611 for (size_t i
= p2
->waiting(); i
> 0; --i
)
613 var_inits
->splice(p3
, *var_inits
, p1
);
619 if (p2
== var_inits
->end())
621 // VAR does not depends upon any other initialization expressions.
623 // Check for a loop of VAR on itself. We only do this if
624 // INIT is not NULL; when INIT is NULL, it means that
625 // PREINIT sets VAR, which we will interpret as a loop.
626 if (init
!= NULL
&& expression_requires(init
, preinit
, var
))
627 error_at(var
->location(),
628 "initialization expression for %qs depends upon itself",
629 var
->message_name().c_str());
630 ready
.splice(ready
.end(), *var_inits
, p1
);
634 // Now READY is the list in the desired initialization order.
635 var_inits
->swap(ready
);
638 // Write out the global definitions.
641 Gogo::write_globals()
643 this->convert_named_types();
644 this->build_interface_method_tables();
646 Bindings
* bindings
= this->current_bindings();
647 size_t count
= bindings
->size_definitions();
649 tree
* vec
= new tree
[count
];
651 tree init_fndecl
= NULL_TREE
;
652 tree init_stmt_list
= NULL_TREE
;
654 if (this->is_main_package())
655 this->init_imports(&init_stmt_list
);
657 // A list of variable initializations.
660 // A list of variables which need to be registered with the garbage
662 std::vector
<Named_object
*> var_gc
;
663 var_gc
.reserve(count
);
665 tree var_init_stmt_list
= NULL_TREE
;
667 for (Bindings::const_definitions_iterator p
= bindings
->begin_definitions();
668 p
!= bindings
->end_definitions();
671 Named_object
* no
= *p
;
673 gcc_assert(!no
->is_type_declaration() && !no
->is_function_declaration());
674 // There is nothing to do for a package.
675 if (no
->is_package())
682 // There is nothing to do for an object which was imported from
683 // a different package into the global scope.
684 if (no
->package() != NULL
)
691 // There is nothing useful we can output for constants which
692 // have ideal or non-integeral type.
695 Type
* type
= no
->const_value()->type();
697 type
= no
->const_value()->expr()->type();
698 if (type
->is_abstract() || type
->integer_type() == NULL
)
706 vec
[i
] = no
->get_tree(this, NULL
);
708 if (vec
[i
] == error_mark_node
)
710 gcc_assert(saw_errors());
716 // If a variable is initialized to a non-constant value, do the
717 // initialization in an initialization function.
718 if (TREE_CODE(vec
[i
]) == VAR_DECL
)
720 gcc_assert(no
->is_variable());
722 // Check for a sink variable, which may be used to run
723 // an initializer purely for its side effects.
724 bool is_sink
= no
->name()[0] == '_' && no
->name()[1] == '.';
726 tree var_init_tree
= NULL_TREE
;
727 if (!no
->var_value()->has_pre_init())
729 tree init
= no
->var_value()->get_init_tree(this, NULL
);
730 if (init
== error_mark_node
)
731 gcc_assert(saw_errors());
732 else if (init
== NULL_TREE
)
734 else if (TREE_CONSTANT(init
))
735 DECL_INITIAL(vec
[i
]) = init
;
737 var_init_tree
= init
;
739 var_init_tree
= fold_build2_loc(no
->location(), MODIFY_EXPR
,
740 void_type_node
, vec
[i
], init
);
744 // We are going to create temporary variables which
745 // means that we need an fndecl.
746 if (init_fndecl
== NULL_TREE
)
747 init_fndecl
= this->initialization_function_decl();
748 current_function_decl
= init_fndecl
;
749 if (DECL_STRUCT_FUNCTION(init_fndecl
) == NULL
)
750 push_struct_function(init_fndecl
);
752 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl
));
754 tree var_decl
= is_sink
? NULL_TREE
: vec
[i
];
755 var_init_tree
= no
->var_value()->get_init_block(this, NULL
,
758 current_function_decl
= NULL_TREE
;
762 if (var_init_tree
!= NULL_TREE
&& var_init_tree
!= error_mark_node
)
764 if (no
->var_value()->init() == NULL
765 && !no
->var_value()->has_pre_init())
766 append_to_statement_list(var_init_tree
, &var_init_stmt_list
);
768 var_inits
.push_back(Var_init(no
, var_init_tree
));
771 if (!is_sink
&& no
->var_value()->type()->has_pointer())
772 var_gc
.push_back(no
);
776 // Register global variables with the garbage collector.
777 this->register_gc_vars(var_gc
, &init_stmt_list
);
779 // Simple variable initializations, after all variables are
781 append_to_statement_list(var_init_stmt_list
, &init_stmt_list
);
783 // Complex variable initializations, first sorting them into a
785 if (!var_inits
.empty())
787 sort_var_inits(&var_inits
);
788 for (Var_inits::const_iterator p
= var_inits
.begin();
789 p
!= var_inits
.end();
791 append_to_statement_list(p
->init(), &init_stmt_list
);
794 // After all the variables are initialized, call the "init"
795 // functions if there are any.
796 for (std::vector
<Named_object
*>::const_iterator p
=
797 this->init_functions_
.begin();
798 p
!= this->init_functions_
.end();
801 tree decl
= (*p
)->get_tree(this, NULL
);
802 tree call
= build_call_expr(decl
, 0);
803 append_to_statement_list(call
, &init_stmt_list
);
806 // Set up a magic function to do all the initialization actions.
807 // This will be called if this package is imported.
808 if (init_stmt_list
!= NULL_TREE
809 || this->need_init_fn_
810 || this->is_main_package())
811 this->write_initialization_function(init_fndecl
, init_stmt_list
);
813 // Pass everything back to the middle-end.
815 wrapup_global_declarations(vec
, count
);
817 cgraph_finalize_compilation_unit();
819 check_global_declarations(vec
, count
);
820 emit_debug_global_declarations(vec
, count
);
825 // Get a tree for the identifier for a named object.
828 Named_object::get_id(Gogo
* gogo
)
830 std::string decl_name
;
831 if (this->is_function_declaration()
832 && !this->func_declaration_value()->asm_name().empty())
833 decl_name
= this->func_declaration_value()->asm_name();
834 else if ((this->is_variable() && !this->var_value()->is_global())
836 && this->type_value()->location() == BUILTINS_LOCATION
))
838 // We don't need the package name for local variables or builtin
840 decl_name
= Gogo::unpack_hidden_name(this->name_
);
844 std::string package_name
;
845 if (this->package_
== NULL
)
846 package_name
= gogo
->package_name();
848 package_name
= this->package_
->name();
850 decl_name
= package_name
+ '.' + Gogo::unpack_hidden_name(this->name_
);
852 Function_type
* fntype
;
853 if (this->is_function())
854 fntype
= this->func_value()->type();
855 else if (this->is_function_declaration())
856 fntype
= this->func_declaration_value()->type();
859 if (fntype
!= NULL
&& fntype
->is_method())
861 decl_name
.push_back('.');
862 decl_name
.append(fntype
->receiver()->type()->mangled_name(gogo
));
867 const Named_object
* in_function
= this->type_value()->in_function();
868 if (in_function
!= NULL
)
869 decl_name
+= '$' + in_function
->name();
871 return get_identifier_from_string(decl_name
);
874 // Get a tree for a named object.
877 Named_object::get_tree(Gogo
* gogo
, Named_object
* function
)
879 if (this->tree_
!= NULL_TREE
)
881 // If this is a variable whose address is taken, we must rebuild
882 // the INDIRECT_REF each time to avoid invalid sharing.
883 tree ret
= this->tree_
;
884 if (((this->classification_
== NAMED_OBJECT_VAR
885 && this->var_value()->is_in_heap())
886 || (this->classification_
== NAMED_OBJECT_RESULT_VAR
887 && this->result_var_value()->is_in_heap()))
888 && ret
!= error_mark_node
)
890 gcc_assert(TREE_CODE(ret
) == INDIRECT_REF
);
891 ret
= build_fold_indirect_ref(TREE_OPERAND(ret
, 0));
892 TREE_THIS_NOTRAP(ret
) = 1;
898 if (this->classification_
== NAMED_OBJECT_TYPE
)
901 name
= this->get_id(gogo
);
903 switch (this->classification_
)
905 case NAMED_OBJECT_CONST
:
907 Named_constant
* named_constant
= this->u_
.const_value
;
908 Translate_context
subcontext(gogo
, function
, NULL
, NULL_TREE
);
909 tree expr_tree
= named_constant
->expr()->get_tree(&subcontext
);
910 if (expr_tree
== error_mark_node
)
911 decl
= error_mark_node
;
914 Type
* type
= named_constant
->type();
915 if (type
!= NULL
&& !type
->is_abstract())
917 if (!type
->is_error())
918 expr_tree
= fold_convert(type
->get_tree(gogo
), expr_tree
);
920 expr_tree
= error_mark_node
;
922 if (expr_tree
== error_mark_node
)
923 decl
= error_mark_node
;
924 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree
)))
926 decl
= build_decl(named_constant
->location(), CONST_DECL
,
927 name
, TREE_TYPE(expr_tree
));
928 DECL_INITIAL(decl
) = expr_tree
;
929 TREE_CONSTANT(decl
) = 1;
930 TREE_READONLY(decl
) = 1;
934 // A CONST_DECL is only for an enum constant, so we
935 // shouldn't use for non-integral types. Instead we
936 // just return the constant itself, rather than a
944 case NAMED_OBJECT_TYPE
:
946 Named_type
* named_type
= this->u_
.type_value
;
947 tree type_tree
= named_type
->get_tree(gogo
);
948 if (type_tree
== error_mark_node
)
949 decl
= error_mark_node
;
952 decl
= TYPE_NAME(type_tree
);
953 gcc_assert(decl
!= NULL_TREE
);
955 // We need to produce a type descriptor for every named
956 // type, and for a pointer to every named type, since
957 // other files or packages might refer to them. We need
958 // to do this even for hidden types, because they might
959 // still be returned by some function. Simply calling the
960 // type_descriptor method is enough to create the type
961 // descriptor, even though we don't do anything with it.
962 if (this->package_
== NULL
)
964 named_type
->type_descriptor_pointer(gogo
);
965 Type
* pn
= Type::make_pointer_type(named_type
);
966 pn
->type_descriptor_pointer(gogo
);
972 case NAMED_OBJECT_TYPE_DECLARATION
:
973 error("reference to undefined type %qs",
974 this->message_name().c_str());
975 return error_mark_node
;
977 case NAMED_OBJECT_VAR
:
979 Variable
* var
= this->u_
.var_value
;
980 Type
* type
= var
->type();
981 if (type
->is_error_type()
982 || (type
->is_undefined()
983 && (!var
->is_global() || this->package() == NULL
)))
985 // Force the error for an undefined type, just in case.
987 decl
= error_mark_node
;
991 tree var_type
= type
->get_tree(gogo
);
992 bool is_parameter
= var
->is_parameter();
993 if (var
->is_receiver() && type
->points_to() == NULL
)
994 is_parameter
= false;
995 if (var
->is_in_heap())
997 is_parameter
= false;
998 var_type
= build_pointer_type(var_type
);
1000 decl
= build_decl(var
->location(),
1001 is_parameter
? PARM_DECL
: VAR_DECL
,
1003 if (!var
->is_global())
1005 tree fnid
= function
->get_id(gogo
);
1006 tree fndecl
= function
->func_value()->get_or_make_decl(gogo
,
1009 DECL_CONTEXT(decl
) = fndecl
;
1012 DECL_ARG_TYPE(decl
) = TREE_TYPE(decl
);
1014 if (var
->is_global())
1016 const Package
* package
= this->package();
1017 if (package
== NULL
)
1018 TREE_STATIC(decl
) = 1;
1020 DECL_EXTERNAL(decl
) = 1;
1021 if (!Gogo::is_hidden_name(this->name_
))
1023 TREE_PUBLIC(decl
) = 1;
1024 std::string asm_name
= (package
== NULL
1025 ? gogo
->unique_prefix()
1026 : package
->unique_prefix());
1027 asm_name
.append(1, '.');
1028 asm_name
.append(IDENTIFIER_POINTER(name
),
1029 IDENTIFIER_LENGTH(name
));
1030 tree asm_id
= get_identifier_from_string(asm_name
);
1031 SET_DECL_ASSEMBLER_NAME(decl
, asm_id
);
1035 // FIXME: We should only set this for variables which are
1036 // actually used somewhere.
1037 TREE_USED(decl
) = 1;
1042 case NAMED_OBJECT_RESULT_VAR
:
1044 Result_variable
* result
= this->u_
.result_var_value
;
1045 Type
* type
= result
->type();
1046 if (type
->is_error())
1047 decl
= error_mark_node
;
1050 gcc_assert(result
->function() == function
->func_value());
1051 source_location loc
= function
->location();
1052 tree result_type
= type
->get_tree(gogo
);
1054 if (!result
->is_in_heap())
1055 init
= type
->get_init_tree(gogo
, false);
1058 tree space
= gogo
->allocate_memory(type
,
1059 TYPE_SIZE_UNIT(result_type
),
1061 result_type
= build_pointer_type(result_type
);
1062 tree subinit
= type
->get_init_tree(gogo
, true);
1063 if (subinit
== NULL_TREE
)
1064 init
= fold_convert_loc(loc
, result_type
, space
);
1067 space
= save_expr(space
);
1068 space
= fold_convert_loc(loc
, result_type
, space
);
1069 tree spaceref
= build_fold_indirect_ref_loc(loc
, space
);
1070 TREE_THIS_NOTRAP(spaceref
) = 1;
1071 tree set
= fold_build2_loc(loc
, MODIFY_EXPR
, void_type_node
,
1073 init
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(space
),
1077 decl
= build_decl(loc
, VAR_DECL
, name
, result_type
);
1078 tree fnid
= function
->get_id(gogo
);
1079 tree fndecl
= function
->func_value()->get_or_make_decl(gogo
,
1082 DECL_CONTEXT(decl
) = fndecl
;
1083 DECL_INITIAL(decl
) = init
;
1084 TREE_USED(decl
) = 1;
1089 case NAMED_OBJECT_SINK
:
1092 case NAMED_OBJECT_FUNC
:
1094 Function
* func
= this->u_
.func_value
;
1095 decl
= func
->get_or_make_decl(gogo
, this, name
);
1096 if (decl
!= error_mark_node
)
1098 if (func
->block() != NULL
)
1100 if (DECL_STRUCT_FUNCTION(decl
) == NULL
)
1101 push_struct_function(decl
);
1103 push_cfun(DECL_STRUCT_FUNCTION(decl
));
1105 cfun
->function_end_locus
= func
->block()->end_location();
1107 current_function_decl
= decl
;
1109 func
->build_tree(gogo
, this);
1111 gimplify_function_tree(decl
);
1113 cgraph_finalize_function(decl
, true);
1115 current_function_decl
= NULL_TREE
;
1126 if (TREE_TYPE(decl
) == error_mark_node
)
1127 decl
= error_mark_node
;
1131 // If this is a local variable whose address is taken, then we
1132 // actually store it in the heap. For uses of the variable we need
1133 // to return a reference to that heap location.
1134 if (((this->classification_
== NAMED_OBJECT_VAR
1135 && this->var_value()->is_in_heap())
1136 || (this->classification_
== NAMED_OBJECT_RESULT_VAR
1137 && this->result_var_value()->is_in_heap()))
1138 && ret
!= error_mark_node
)
1140 gcc_assert(POINTER_TYPE_P(TREE_TYPE(ret
)));
1141 ret
= build_fold_indirect_ref(ret
);
1142 TREE_THIS_NOTRAP(ret
) = 1;
1147 if (ret
!= error_mark_node
)
1148 go_preserve_from_gc(ret
);
1153 // Get the initial value of a variable as a tree. This does not
1154 // consider whether the variable is in the heap--it returns the
1155 // initial value as though it were always stored in the stack.
1158 Variable::get_init_tree(Gogo
* gogo
, Named_object
* function
)
1160 gcc_assert(this->preinit_
== NULL
);
1161 if (this->init_
== NULL
)
1163 gcc_assert(!this->is_parameter_
);
1164 return this->type_
->get_init_tree(gogo
, this->is_global_
);
1168 Translate_context
context(gogo
, function
, NULL
, NULL_TREE
);
1169 tree rhs_tree
= this->init_
->get_tree(&context
);
1170 return Expression::convert_for_assignment(&context
, this->type(),
1171 this->init_
->type(),
1172 rhs_tree
, this->location());
1176 // Get the initial value of a variable when a block is required.
1177 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1180 Variable::get_init_block(Gogo
* gogo
, Named_object
* function
, tree var_decl
)
1182 gcc_assert(this->preinit_
!= NULL
);
1184 // We want to add the variable assignment to the end of the preinit
1185 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1186 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1187 // regular statements.
1189 Translate_context
context(gogo
, function
, NULL
, NULL_TREE
);
1190 tree block_tree
= this->preinit_
->get_tree(&context
);
1191 if (block_tree
== error_mark_node
)
1192 return error_mark_node
;
1193 gcc_assert(TREE_CODE(block_tree
) == BIND_EXPR
);
1194 tree statements
= BIND_EXPR_BODY(block_tree
);
1195 while (statements
!= NULL_TREE
1196 && (TREE_CODE(statements
) == TRY_FINALLY_EXPR
1197 || TREE_CODE(statements
) == TRY_CATCH_EXPR
))
1198 statements
= TREE_OPERAND(statements
, 0);
1200 // It's possible to have pre-init statements without an initializer
1201 // if the pre-init statements set the variable.
1202 if (this->init_
!= NULL
)
1204 tree rhs_tree
= this->init_
->get_tree(&context
);
1205 if (rhs_tree
== error_mark_node
)
1206 return error_mark_node
;
1207 if (var_decl
== NULL_TREE
)
1208 append_to_statement_list(rhs_tree
, &statements
);
1211 tree val
= Expression::convert_for_assignment(&context
, this->type(),
1212 this->init_
->type(),
1215 if (val
== error_mark_node
)
1216 return error_mark_node
;
1217 tree set
= fold_build2_loc(this->location(), MODIFY_EXPR
,
1218 void_type_node
, var_decl
, val
);
1219 append_to_statement_list(set
, &statements
);
1226 // Get a tree for a function decl.
1229 Function::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1231 if (this->fndecl_
== NULL_TREE
)
1233 tree functype
= this->type_
->get_tree(gogo
);
1234 if (functype
== error_mark_node
)
1235 this->fndecl_
= error_mark_node
;
1238 // The type of a function comes back as a pointer, but we
1239 // want the real function type for a function declaration.
1240 gcc_assert(POINTER_TYPE_P(functype
));
1241 functype
= TREE_TYPE(functype
);
1242 tree decl
= build_decl(this->location(), FUNCTION_DECL
, id
, functype
);
1244 this->fndecl_
= decl
;
1246 if (no
->package() != NULL
)
1248 else if (this->enclosing_
!= NULL
|| Gogo::is_thunk(no
))
1250 else if (Gogo::unpack_hidden_name(no
->name()) == "init"
1251 && !this->type_
->is_method())
1253 else if (Gogo::unpack_hidden_name(no
->name()) == "main"
1254 && gogo
->is_main_package())
1255 TREE_PUBLIC(decl
) = 1;
1256 // Methods have to be public even if they are hidden because
1257 // they can be pulled into type descriptors when using
1258 // anonymous fields.
1259 else if (!Gogo::is_hidden_name(no
->name())
1260 || this->type_
->is_method())
1262 TREE_PUBLIC(decl
) = 1;
1263 std::string asm_name
= gogo
->unique_prefix();
1264 asm_name
.append(1, '.');
1265 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1266 SET_DECL_ASSEMBLER_NAME(decl
,
1267 get_identifier_from_string(asm_name
));
1270 // Why do we have to do this in the frontend?
1271 tree restype
= TREE_TYPE(functype
);
1272 tree resdecl
= build_decl(this->location(), RESULT_DECL
, NULL_TREE
,
1274 DECL_ARTIFICIAL(resdecl
) = 1;
1275 DECL_IGNORED_P(resdecl
) = 1;
1276 DECL_CONTEXT(resdecl
) = decl
;
1277 DECL_RESULT(decl
) = resdecl
;
1279 if (this->enclosing_
!= NULL
)
1280 DECL_STATIC_CHAIN(decl
) = 1;
1282 // If a function calls the predeclared recover function, we
1283 // can't inline it, because recover behaves differently in a
1284 // function passed directly to defer.
1285 if (this->calls_recover_
&& !this->is_recover_thunk_
)
1286 DECL_UNINLINABLE(decl
) = 1;
1288 // If this is a thunk created to call a function which calls
1289 // the predeclared recover function, we need to disable
1290 // stack splitting for the thunk.
1291 if (this->is_recover_thunk_
)
1293 tree attr
= get_identifier("__no_split_stack__");
1294 DECL_ATTRIBUTES(decl
) = tree_cons(attr
, NULL_TREE
, NULL_TREE
);
1297 go_preserve_from_gc(decl
);
1299 if (this->closure_var_
!= NULL
)
1301 push_struct_function(decl
);
1303 tree closure_decl
= this->closure_var_
->get_tree(gogo
, no
);
1304 if (closure_decl
== error_mark_node
)
1305 this->fndecl_
= error_mark_node
;
1308 DECL_ARTIFICIAL(closure_decl
) = 1;
1309 DECL_IGNORED_P(closure_decl
) = 1;
1310 TREE_USED(closure_decl
) = 1;
1311 DECL_ARG_TYPE(closure_decl
) = TREE_TYPE(closure_decl
);
1312 TREE_READONLY(closure_decl
) = 1;
1314 DECL_STRUCT_FUNCTION(decl
)->static_chain_decl
= closure_decl
;
1321 return this->fndecl_
;
1324 // Get a tree for a function declaration.
1327 Function_declaration::get_or_make_decl(Gogo
* gogo
, Named_object
* no
, tree id
)
1329 if (this->fndecl_
== NULL_TREE
)
1331 // Let Go code use an asm declaration to pick up a builtin
1333 if (!this->asm_name_
.empty())
1335 std::map
<std::string
, tree
>::const_iterator p
=
1336 builtin_functions
.find(this->asm_name_
);
1337 if (p
!= builtin_functions
.end())
1339 this->fndecl_
= p
->second
;
1340 return this->fndecl_
;
1344 tree functype
= this->fntype_
->get_tree(gogo
);
1346 if (functype
== error_mark_node
)
1347 decl
= error_mark_node
;
1350 // The type of a function comes back as a pointer, but we
1351 // want the real function type for a function declaration.
1352 gcc_assert(POINTER_TYPE_P(functype
));
1353 functype
= TREE_TYPE(functype
);
1354 decl
= build_decl(this->location(), FUNCTION_DECL
, id
, functype
);
1355 TREE_PUBLIC(decl
) = 1;
1356 DECL_EXTERNAL(decl
) = 1;
1358 if (this->asm_name_
.empty())
1360 std::string asm_name
= (no
->package() == NULL
1361 ? gogo
->unique_prefix()
1362 : no
->package()->unique_prefix());
1363 asm_name
.append(1, '.');
1364 asm_name
.append(IDENTIFIER_POINTER(id
), IDENTIFIER_LENGTH(id
));
1365 SET_DECL_ASSEMBLER_NAME(decl
,
1366 get_identifier_from_string(asm_name
));
1369 this->fndecl_
= decl
;
1370 go_preserve_from_gc(decl
);
1372 return this->fndecl_
;
1375 // We always pass the receiver to a method as a pointer. If the
1376 // receiver is actually declared as a non-pointer type, then we copy
1377 // the value into a local variable, so that it has the right type. In
1378 // this function we create the real PARM_DECL to use, and set
1379 // DEC_INITIAL of the var_decl to be the value passed in.
1382 Function::make_receiver_parm_decl(Gogo
* gogo
, Named_object
* no
, tree var_decl
)
1384 if (var_decl
== error_mark_node
)
1385 return error_mark_node
;
1386 // If the function takes the address of a receiver which is passed
1387 // by value, then we will have an INDIRECT_REF here. We need to get
1388 // the real variable.
1389 bool is_in_heap
= no
->var_value()->is_in_heap();
1391 if (TREE_CODE(var_decl
) != INDIRECT_REF
)
1393 gcc_assert(!is_in_heap
);
1394 val_type
= TREE_TYPE(var_decl
);
1398 gcc_assert(is_in_heap
);
1399 var_decl
= TREE_OPERAND(var_decl
, 0);
1400 if (var_decl
== error_mark_node
)
1401 return error_mark_node
;
1402 gcc_assert(POINTER_TYPE_P(TREE_TYPE(var_decl
)));
1403 val_type
= TREE_TYPE(TREE_TYPE(var_decl
));
1405 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1406 source_location loc
= DECL_SOURCE_LOCATION(var_decl
);
1407 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1409 tree id
= get_identifier_from_string(name
);
1410 tree parm_decl
= build_decl(loc
, PARM_DECL
, id
, build_pointer_type(val_type
));
1411 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1412 DECL_ARG_TYPE(parm_decl
) = TREE_TYPE(parm_decl
);
1414 gcc_assert(DECL_INITIAL(var_decl
) == NULL_TREE
);
1415 // The receiver might be passed as a null pointer.
1416 tree check
= fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
, parm_decl
,
1417 fold_convert_loc(loc
, TREE_TYPE(parm_decl
),
1418 null_pointer_node
));
1419 tree ind
= build_fold_indirect_ref_loc(loc
, parm_decl
);
1420 TREE_THIS_NOTRAP(ind
) = 1;
1421 tree zero_init
= no
->var_value()->type()->get_init_tree(gogo
, false);
1422 tree init
= fold_build3_loc(loc
, COND_EXPR
, TREE_TYPE(ind
),
1423 check
, ind
, zero_init
);
1427 tree size
= TYPE_SIZE_UNIT(val_type
);
1428 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
,
1430 space
= save_expr(space
);
1431 space
= fold_convert(build_pointer_type(val_type
), space
);
1432 tree spaceref
= build_fold_indirect_ref_loc(no
->location(), space
);
1433 TREE_THIS_NOTRAP(spaceref
) = 1;
1434 tree check
= fold_build2_loc(loc
, NE_EXPR
, boolean_type_node
,
1436 fold_convert_loc(loc
, TREE_TYPE(parm_decl
),
1437 null_pointer_node
));
1438 tree parmref
= build_fold_indirect_ref_loc(no
->location(), parm_decl
);
1439 TREE_THIS_NOTRAP(parmref
) = 1;
1440 tree set
= fold_build2_loc(loc
, MODIFY_EXPR
, void_type_node
,
1442 init
= fold_build2_loc(loc
, COMPOUND_EXPR
, TREE_TYPE(space
),
1443 build3(COND_EXPR
, void_type_node
,
1444 check
, set
, NULL_TREE
),
1448 DECL_INITIAL(var_decl
) = init
;
1453 // If we take the address of a parameter, then we need to copy it into
1454 // the heap. We will access it as a local variable via an
1458 Function::copy_parm_to_heap(Gogo
* gogo
, Named_object
* no
, tree ref
)
1460 if (ref
== error_mark_node
)
1461 return error_mark_node
;
1463 gcc_assert(TREE_CODE(ref
) == INDIRECT_REF
);
1465 tree var_decl
= TREE_OPERAND(ref
, 0);
1466 if (var_decl
== error_mark_node
)
1467 return error_mark_node
;
1468 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1469 source_location loc
= DECL_SOURCE_LOCATION(var_decl
);
1471 std::string name
= IDENTIFIER_POINTER(DECL_NAME(var_decl
));
1473 tree id
= get_identifier_from_string(name
);
1475 tree type
= TREE_TYPE(var_decl
);
1476 gcc_assert(POINTER_TYPE_P(type
));
1477 type
= TREE_TYPE(type
);
1479 tree parm_decl
= build_decl(loc
, PARM_DECL
, id
, type
);
1480 DECL_CONTEXT(parm_decl
) = current_function_decl
;
1481 DECL_ARG_TYPE(parm_decl
) = type
;
1483 tree size
= TYPE_SIZE_UNIT(type
);
1484 tree space
= gogo
->allocate_memory(no
->var_value()->type(), size
, loc
);
1485 space
= save_expr(space
);
1486 space
= fold_convert(TREE_TYPE(var_decl
), space
);
1487 tree spaceref
= build_fold_indirect_ref_loc(loc
, space
);
1488 TREE_THIS_NOTRAP(spaceref
) = 1;
1489 tree init
= build2(COMPOUND_EXPR
, TREE_TYPE(space
),
1490 build2(MODIFY_EXPR
, void_type_node
, spaceref
, parm_decl
),
1492 DECL_INITIAL(var_decl
) = init
;
1497 // Get a tree for function code.
1500 Function::build_tree(Gogo
* gogo
, Named_object
* named_function
)
1502 tree fndecl
= this->fndecl_
;
1503 gcc_assert(fndecl
!= NULL_TREE
);
1505 tree params
= NULL_TREE
;
1508 tree declare_vars
= NULL_TREE
;
1509 for (Bindings::const_definitions_iterator p
=
1510 this->block_
->bindings()->begin_definitions();
1511 p
!= this->block_
->bindings()->end_definitions();
1514 if ((*p
)->is_variable() && (*p
)->var_value()->is_parameter())
1516 *pp
= (*p
)->get_tree(gogo
, named_function
);
1518 // We always pass the receiver to a method as a pointer. If
1519 // the receiver is declared as a non-pointer type, then we
1520 // copy the value into a local variable.
1521 if ((*p
)->var_value()->is_receiver()
1522 && (*p
)->var_value()->type()->points_to() == NULL
)
1524 tree parm_decl
= this->make_receiver_parm_decl(gogo
, *p
, *pp
);
1526 if (TREE_CODE(var
) == INDIRECT_REF
)
1527 var
= TREE_OPERAND(var
, 0);
1528 if (var
!= error_mark_node
)
1530 gcc_assert(TREE_CODE(var
) == VAR_DECL
);
1531 DECL_CHAIN(var
) = declare_vars
;
1536 else if ((*p
)->var_value()->is_in_heap())
1538 // If we take the address of a parameter, then we need
1539 // to copy it into the heap.
1540 tree parm_decl
= this->copy_parm_to_heap(gogo
, *p
, *pp
);
1541 if (*pp
!= error_mark_node
)
1543 gcc_assert(TREE_CODE(*pp
) == INDIRECT_REF
);
1544 tree var_decl
= TREE_OPERAND(*pp
, 0);
1545 if (var_decl
!= error_mark_node
)
1547 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1548 DECL_CHAIN(var_decl
) = declare_vars
;
1549 declare_vars
= var_decl
;
1555 if (*pp
!= error_mark_node
)
1557 gcc_assert(TREE_CODE(*pp
) == PARM_DECL
);
1558 pp
= &DECL_CHAIN(*pp
);
1561 else if ((*p
)->is_result_variable())
1563 tree var_decl
= (*p
)->get_tree(gogo
, named_function
);
1564 if (var_decl
!= error_mark_node
1565 && (*p
)->result_var_value()->is_in_heap())
1567 gcc_assert(TREE_CODE(var_decl
) == INDIRECT_REF
);
1568 var_decl
= TREE_OPERAND(var_decl
, 0);
1570 if (var_decl
!= error_mark_node
)
1572 gcc_assert(TREE_CODE(var_decl
) == VAR_DECL
);
1573 DECL_CHAIN(var_decl
) = declare_vars
;
1574 declare_vars
= var_decl
;
1580 DECL_ARGUMENTS(fndecl
) = params
;
1582 if (this->block_
!= NULL
)
1584 gcc_assert(DECL_INITIAL(fndecl
) == NULL_TREE
);
1586 // Declare variables if necessary.
1587 tree bind
= NULL_TREE
;
1588 if (declare_vars
!= NULL_TREE
)
1590 tree block
= make_node(BLOCK
);
1591 BLOCK_SUPERCONTEXT(block
) = fndecl
;
1592 DECL_INITIAL(fndecl
) = block
;
1593 BLOCK_VARS(block
) = declare_vars
;
1594 TREE_USED(block
) = 1;
1595 bind
= build3(BIND_EXPR
, void_type_node
, BLOCK_VARS(block
),
1597 TREE_SIDE_EFFECTS(bind
) = 1;
1600 // Build the trees for all the statements in the function.
1601 Translate_context
context(gogo
, named_function
, NULL
, NULL_TREE
);
1602 tree code
= this->block_
->get_tree(&context
);
1604 tree init
= NULL_TREE
;
1605 tree except
= NULL_TREE
;
1606 tree fini
= NULL_TREE
;
1608 // Initialize variables if necessary.
1609 for (tree v
= declare_vars
; v
!= NULL_TREE
; v
= DECL_CHAIN(v
))
1611 tree dv
= build1(DECL_EXPR
, void_type_node
, v
);
1612 SET_EXPR_LOCATION(dv
, DECL_SOURCE_LOCATION(v
));
1613 append_to_statement_list(dv
, &init
);
1616 // If we have a defer stack, initialize it at the start of a
1618 if (this->defer_stack_
!= NULL_TREE
)
1620 tree defer_init
= build1(DECL_EXPR
, void_type_node
,
1621 this->defer_stack_
);
1622 SET_EXPR_LOCATION(defer_init
, this->block_
->start_location());
1623 append_to_statement_list(defer_init
, &init
);
1625 // Clean up the defer stack when we leave the function.
1626 this->build_defer_wrapper(gogo
, named_function
, &except
, &fini
);
1629 if (code
!= NULL_TREE
&& code
!= error_mark_node
)
1631 if (init
!= NULL_TREE
)
1632 code
= build2(COMPOUND_EXPR
, void_type_node
, init
, code
);
1633 if (except
!= NULL_TREE
)
1634 code
= build2(TRY_CATCH_EXPR
, void_type_node
, code
,
1635 build2(CATCH_EXPR
, void_type_node
, NULL
, except
));
1636 if (fini
!= NULL_TREE
)
1637 code
= build2(TRY_FINALLY_EXPR
, void_type_node
, code
, fini
);
1640 // Stick the code into the block we built for the receiver, if
1642 if (bind
!= NULL_TREE
&& code
!= NULL_TREE
&& code
!= error_mark_node
)
1644 BIND_EXPR_BODY(bind
) = code
;
1648 DECL_SAVED_TREE(fndecl
) = code
;
1652 // Build the wrappers around function code needed if the function has
1653 // any defer statements. This sets *EXCEPT to an exception handler
1654 // and *FINI to a finally handler.
1657 Function::build_defer_wrapper(Gogo
* gogo
, Named_object
* named_function
,
1658 tree
*except
, tree
*fini
)
1660 source_location end_loc
= this->block_
->end_location();
1662 // Add an exception handler. This is used if a panic occurs. Its
1663 // purpose is to stop the stack unwinding if a deferred function
1664 // calls recover. There are more details in
1665 // libgo/runtime/go-unwind.c.
1666 tree stmt_list
= NULL_TREE
;
1667 static tree check_fndecl
;
1668 tree call
= Gogo::call_builtin(&check_fndecl
,
1674 this->defer_stack(end_loc
));
1675 if (call
!= error_mark_node
)
1676 append_to_statement_list(call
, &stmt_list
);
1678 tree retval
= this->return_value(gogo
, named_function
, end_loc
, &stmt_list
);
1680 if (retval
== NULL_TREE
)
1683 set
= fold_build2_loc(end_loc
, MODIFY_EXPR
, void_type_node
,
1684 DECL_RESULT(this->fndecl_
), retval
);
1685 tree ret_stmt
= fold_build1_loc(end_loc
, RETURN_EXPR
, void_type_node
, set
);
1686 append_to_statement_list(ret_stmt
, &stmt_list
);
1688 gcc_assert(*except
== NULL_TREE
);
1689 *except
= stmt_list
;
1691 // Add some finally code to run the defer functions. This is used
1692 // both in the normal case, when no panic occurs, and also if a
1693 // panic occurs to run any further defer functions. Of course, it
1694 // is possible for a defer function to call panic which should be
1695 // caught by another defer function. To handle that we use a loop.
1697 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1698 // if (return values are named) return named_vals;
1702 tree label
= create_artificial_label(end_loc
);
1703 tree define_label
= fold_build1_loc(end_loc
, LABEL_EXPR
, void_type_node
,
1705 append_to_statement_list(define_label
, &stmt_list
);
1707 static tree undefer_fndecl
;
1708 tree undefer
= Gogo::call_builtin(&undefer_fndecl
,
1714 this->defer_stack(end_loc
));
1715 if (undefer_fndecl
!= NULL_TREE
)
1716 TREE_NOTHROW(undefer_fndecl
) = 0;
1718 tree defer
= Gogo::call_builtin(&check_fndecl
,
1724 this->defer_stack(end_loc
));
1725 tree jump
= fold_build1_loc(end_loc
, GOTO_EXPR
, void_type_node
, label
);
1726 tree catch_body
= build2(COMPOUND_EXPR
, void_type_node
, defer
, jump
);
1727 catch_body
= build2(CATCH_EXPR
, void_type_node
, NULL
, catch_body
);
1728 tree try_catch
= build2(TRY_CATCH_EXPR
, void_type_node
, undefer
, catch_body
);
1730 append_to_statement_list(try_catch
, &stmt_list
);
1732 if (this->type_
->results() != NULL
1733 && !this->type_
->results()->empty()
1734 && !this->type_
->results()->front().name().empty())
1736 // If the result variables are named, we need to return them
1737 // again, because they might have been changed by a defer
1739 retval
= this->return_value(gogo
, named_function
, end_loc
,
1741 set
= fold_build2_loc(end_loc
, MODIFY_EXPR
, void_type_node
,
1742 DECL_RESULT(this->fndecl_
), retval
);
1743 ret_stmt
= fold_build1_loc(end_loc
, RETURN_EXPR
, void_type_node
, set
);
1744 append_to_statement_list(ret_stmt
, &stmt_list
);
1747 gcc_assert(*fini
== NULL_TREE
);
1751 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1752 // also add statements to STMT_LIST, which need to be executed before
1753 // the assignment. This is used for a return statement with no
1757 Function::return_value(Gogo
* gogo
, Named_object
* named_function
,
1758 source_location location
, tree
* stmt_list
) const
1760 const Typed_identifier_list
* results
= this->type_
->results();
1761 if (results
== NULL
|| results
->empty())
1764 gcc_assert(this->results_
!= NULL
);
1765 if (this->results_
->size() != results
->size())
1767 gcc_assert(saw_errors());
1768 return error_mark_node
;
1772 if (results
->size() == 1)
1773 return this->results_
->front()->get_tree(gogo
, named_function
);
1776 tree rettype
= TREE_TYPE(DECL_RESULT(this->fndecl_
));
1777 retval
= create_tmp_var(rettype
, "RESULT");
1778 tree field
= TYPE_FIELDS(rettype
);
1780 for (Typed_identifier_list::const_iterator pr
= results
->begin();
1781 pr
!= results
->end();
1782 ++pr
, ++index
, field
= DECL_CHAIN(field
))
1784 gcc_assert(field
!= NULL
);
1786 val
= (*this->results_
)[index
]->get_tree(gogo
, named_function
);
1787 tree set
= fold_build2_loc(location
, MODIFY_EXPR
, void_type_node
,
1788 build3(COMPONENT_REF
, TREE_TYPE(field
),
1789 retval
, field
, NULL_TREE
),
1791 append_to_statement_list(set
, stmt_list
);
1797 // Get the tree for the variable holding the defer stack for this
1798 // function. At least at present, the value of this variable is not
1799 // used. However, a pointer to this variable is used as a marker for
1800 // the functions on the defer stack associated with this function.
1801 // Doing things this way permits inlining a function which uses defer.
1804 Function::defer_stack(source_location location
)
1806 if (this->defer_stack_
== NULL_TREE
)
1808 tree var
= create_tmp_var(ptr_type_node
, "DEFER");
1809 DECL_INITIAL(var
) = null_pointer_node
;
1810 DECL_SOURCE_LOCATION(var
) = location
;
1811 TREE_ADDRESSABLE(var
) = 1;
1812 this->defer_stack_
= var
;
1814 return fold_convert_loc(location
, ptr_type_node
,
1815 build_fold_addr_expr_loc(location
,
1816 this->defer_stack_
));
1819 // Get a tree for the statements in a block.
1822 Block::get_tree(Translate_context
* context
)
1824 Gogo
* gogo
= context
->gogo();
1826 tree block
= make_node(BLOCK
);
1828 // Put the new block into the block tree.
1830 if (context
->block() == NULL
)
1833 if (context
->function() != NULL
)
1834 fndecl
= context
->function()->func_value()->get_decl();
1836 fndecl
= current_function_decl
;
1837 gcc_assert(fndecl
!= NULL_TREE
);
1839 // We may have already created a block for the receiver.
1840 if (DECL_INITIAL(fndecl
) == NULL_TREE
)
1842 BLOCK_SUPERCONTEXT(block
) = fndecl
;
1843 DECL_INITIAL(fndecl
) = block
;
1847 tree superblock_tree
= DECL_INITIAL(fndecl
);
1848 BLOCK_SUPERCONTEXT(block
) = superblock_tree
;
1849 gcc_assert(BLOCK_CHAIN(block
) == NULL_TREE
);
1850 BLOCK_CHAIN(block
) = block
;
1855 tree superblock_tree
= context
->block_tree();
1856 BLOCK_SUPERCONTEXT(block
) = superblock_tree
;
1858 for (pp
= &BLOCK_SUBBLOCKS(superblock_tree
);
1860 pp
= &BLOCK_CHAIN(*pp
))
1865 // Expand local variables in the block.
1867 tree
* pp
= &BLOCK_VARS(block
);
1868 for (Bindings::const_definitions_iterator pv
=
1869 this->bindings_
->begin_definitions();
1870 pv
!= this->bindings_
->end_definitions();
1873 if ((!(*pv
)->is_variable() || !(*pv
)->var_value()->is_parameter())
1874 && !(*pv
)->is_result_variable()
1875 && !(*pv
)->is_const())
1877 tree var
= (*pv
)->get_tree(gogo
, context
->function());
1878 if (var
!= error_mark_node
&& TREE_TYPE(var
) != error_mark_node
)
1880 if ((*pv
)->is_variable() && (*pv
)->var_value()->is_in_heap())
1882 gcc_assert(TREE_CODE(var
) == INDIRECT_REF
);
1883 var
= TREE_OPERAND(var
, 0);
1884 gcc_assert(TREE_CODE(var
) == VAR_DECL
);
1887 pp
= &DECL_CHAIN(*pp
);
1893 Translate_context
subcontext(context
->gogo(), context
->function(),
1896 tree statements
= NULL_TREE
;
1898 // Expand the statements.
1900 for (std::vector
<Statement
*>::const_iterator p
= this->statements_
.begin();
1901 p
!= this->statements_
.end();
1904 tree statement
= (*p
)->get_tree(&subcontext
);
1905 if (statement
!= error_mark_node
)
1906 append_to_statement_list(statement
, &statements
);
1909 TREE_USED(block
) = 1;
1911 tree bind
= build3(BIND_EXPR
, void_type_node
, BLOCK_VARS(block
), statements
,
1913 TREE_SIDE_EFFECTS(bind
) = 1;
1918 // Return the integer type to use for a size.
1922 go_type_for_size(unsigned int bits
, int unsignedp
)
1928 name
= unsignedp
? "uint8" : "int8";
1931 name
= unsignedp
? "uint16" : "int16";
1934 name
= unsignedp
? "uint32" : "int32";
1937 name
= unsignedp
? "uint64" : "int64";
1940 if (bits
== POINTER_SIZE
&& unsignedp
)
1945 Type
* type
= Type::lookup_integer_type(name
);
1946 return type
->get_tree(go_get_gogo());
1949 // Return the type to use for a mode.
1953 go_type_for_mode(enum machine_mode mode
, int unsignedp
)
1955 // FIXME: This static_cast should be in machmode.h.
1956 enum mode_class mc
= static_cast<enum mode_class
>(GET_MODE_CLASS(mode
));
1958 return go_type_for_size(GET_MODE_BITSIZE(mode
), unsignedp
);
1959 else if (mc
== MODE_FLOAT
)
1962 switch (GET_MODE_BITSIZE (mode
))
1965 type
= Type::lookup_float_type("float32");
1968 type
= Type::lookup_float_type("float64");
1971 // We have to check for long double in order to support
1972 // i386 excess precision.
1973 if (mode
== TYPE_MODE(long_double_type_node
))
1974 return long_double_type_node
;
1977 return type
->float_type()->type_tree();
1979 else if (mc
== MODE_COMPLEX_FLOAT
)
1982 switch (GET_MODE_BITSIZE (mode
))
1985 type
= Type::lookup_complex_type("complex64");
1988 type
= Type::lookup_complex_type("complex128");
1991 // We have to check for long double in order to support
1992 // i386 excess precision.
1993 if (mode
== TYPE_MODE(complex_long_double_type_node
))
1994 return complex_long_double_type_node
;
1997 return type
->complex_type()->type_tree();
2003 // Return a tree which allocates SIZE bytes which will holds value of
2007 Gogo::allocate_memory(Type
* type
, tree size
, source_location location
)
2009 // If the package imports unsafe, then it may play games with
2010 // pointers that look like integers.
2011 if (this->imported_unsafe_
|| type
->has_pointer())
2013 static tree new_fndecl
;
2014 return Gogo::call_builtin(&new_fndecl
,
2024 static tree new_nopointers_fndecl
;
2025 return Gogo::call_builtin(&new_nopointers_fndecl
,
2027 "__go_new_nopointers",
2035 // Build a builtin struct with a list of fields. The name is
2036 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
2037 // node; this exists so that the struct can have fields which point to
2038 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
2039 // are NFIELDS fields. Each field is a name (a const char*) followed
2040 // by a type (a tree).
2043 Gogo::builtin_struct(tree
* ptype
, const char* struct_name
, tree struct_type
,
2046 if (ptype
!= NULL
&& *ptype
!= NULL_TREE
)
2050 va_start(ap
, nfields
);
2052 tree fields
= NULL_TREE
;
2053 for (int i
= 0; i
< nfields
; ++i
)
2055 const char* field_name
= va_arg(ap
, const char*);
2056 tree type
= va_arg(ap
, tree
);
2057 if (type
== error_mark_node
)
2060 *ptype
= error_mark_node
;
2061 return error_mark_node
;
2063 tree field
= build_decl(BUILTINS_LOCATION
, FIELD_DECL
,
2064 get_identifier(field_name
), type
);
2065 DECL_CHAIN(field
) = fields
;
2071 if (struct_type
== NULL_TREE
)
2072 struct_type
= make_node(RECORD_TYPE
);
2073 finish_builtin_struct(struct_type
, struct_name
, fields
, NULL_TREE
);
2077 go_preserve_from_gc(struct_type
);
2078 *ptype
= struct_type
;
2084 // Return a type to use for pointer to const char for a string.
2087 Gogo::const_char_pointer_type_tree()
2090 if (type
== NULL_TREE
)
2092 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
2094 type
= build_pointer_type(const_char_type
);
2095 go_preserve_from_gc(type
);
2100 // Return a tree for a string constant.
2103 Gogo::string_constant_tree(const std::string
& val
)
2105 tree index_type
= build_index_type(size_int(val
.length()));
2106 tree const_char_type
= build_qualified_type(unsigned_char_type_node
,
2108 tree string_type
= build_array_type(const_char_type
, index_type
);
2109 string_type
= build_variant_type_copy(string_type
);
2110 TYPE_STRING_FLAG(string_type
) = 1;
2111 tree string_val
= build_string(val
.length(), val
.data());
2112 TREE_TYPE(string_val
) = string_type
;
2116 // Return a tree for a Go string constant.
2119 Gogo::go_string_constant_tree(const std::string
& val
)
2121 tree string_type
= Type::make_string_type()->get_tree(this);
2123 VEC(constructor_elt
, gc
)* init
= VEC_alloc(constructor_elt
, gc
, 2);
2125 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2126 tree field
= TYPE_FIELDS(string_type
);
2127 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__data") == 0);
2129 tree str
= Gogo::string_constant_tree(val
);
2130 elt
->value
= fold_convert(TREE_TYPE(field
),
2131 build_fold_addr_expr(str
));
2133 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2134 field
= DECL_CHAIN(field
);
2135 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__length") == 0);
2137 elt
->value
= build_int_cst_type(TREE_TYPE(field
), val
.length());
2139 tree constructor
= build_constructor(string_type
, init
);
2140 TREE_READONLY(constructor
) = 1;
2141 TREE_CONSTANT(constructor
) = 1;
2146 // Return a tree for a pointer to a Go string constant. This is only
2147 // used for type descriptors, so we return a pointer to a constant
2151 Gogo::ptr_go_string_constant_tree(const std::string
& val
)
2153 tree pval
= this->go_string_constant_tree(val
);
2155 tree decl
= build_decl(UNKNOWN_LOCATION
, VAR_DECL
,
2156 create_tmp_var_name("SP"), TREE_TYPE(pval
));
2157 DECL_EXTERNAL(decl
) = 0;
2158 TREE_PUBLIC(decl
) = 0;
2159 TREE_USED(decl
) = 1;
2160 TREE_READONLY(decl
) = 1;
2161 TREE_CONSTANT(decl
) = 1;
2162 TREE_STATIC(decl
) = 1;
2163 DECL_ARTIFICIAL(decl
) = 1;
2164 DECL_INITIAL(decl
) = pval
;
2165 rest_of_decl_compilation(decl
, 1, 0);
2167 return build_fold_addr_expr(decl
);
2170 // Build the type of the struct that holds a slice for the given
2174 Gogo::slice_type_tree(tree element_type_tree
)
2176 // We use int for the count and capacity fields in a slice header.
2177 // This matches 6g. The language definition guarantees that we
2178 // can't allocate space of a size which does not fit in int
2179 // anyhow. FIXME: integer_type_node is the the C type "int" but is
2180 // not necessarily the Go type "int". They will differ when the C
2181 // type "int" has fewer than 32 bits.
2182 return Gogo::builtin_struct(NULL
, "__go_slice", NULL_TREE
, 3,
2184 build_pointer_type(element_type_tree
),
2191 // Given the tree for a slice type, return the tree for the type of
2192 // the elements of the slice.
2195 Gogo::slice_element_type_tree(tree slice_type_tree
)
2197 gcc_assert(TREE_CODE(slice_type_tree
) == RECORD_TYPE
2198 && POINTER_TYPE_P(TREE_TYPE(TYPE_FIELDS(slice_type_tree
))));
2199 return TREE_TYPE(TREE_TYPE(TYPE_FIELDS(slice_type_tree
)));
2202 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2203 // the slice. VALUES is the value pointer and COUNT is the number of
2204 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2205 // the capacity and the count are the same.
2208 Gogo::slice_constructor(tree slice_type_tree
, tree values
, tree count
,
2211 gcc_assert(TREE_CODE(slice_type_tree
) == RECORD_TYPE
);
2213 VEC(constructor_elt
,gc
)* init
= VEC_alloc(constructor_elt
, gc
, 3);
2215 tree field
= TYPE_FIELDS(slice_type_tree
);
2216 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__values") == 0);
2217 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2219 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field
))
2220 == TYPE_MAIN_VARIANT(TREE_TYPE(values
)));
2221 elt
->value
= values
;
2223 count
= fold_convert(sizetype
, count
);
2224 if (capacity
== NULL_TREE
)
2226 count
= save_expr(count
);
2230 field
= DECL_CHAIN(field
);
2231 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__count") == 0);
2232 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2234 elt
->value
= fold_convert(TREE_TYPE(field
), count
);
2236 field
= DECL_CHAIN(field
);
2237 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field
)), "__capacity") == 0);
2238 elt
= VEC_quick_push(constructor_elt
, init
, NULL
);
2240 elt
->value
= fold_convert(TREE_TYPE(field
), capacity
);
2242 return build_constructor(slice_type_tree
, init
);
2245 // Build a constructor for an empty slice.
2248 Gogo::empty_slice_constructor(tree slice_type_tree
)
2250 tree element_field
= TYPE_FIELDS(slice_type_tree
);
2251 tree ret
= Gogo::slice_constructor(slice_type_tree
,
2252 fold_convert(TREE_TYPE(element_field
),
2256 TREE_CONSTANT(ret
) = 1;
2260 // Build a map descriptor for a map of type MAPTYPE.
2263 Gogo::map_descriptor(Map_type
* maptype
)
2265 if (this->map_descriptors_
== NULL
)
2266 this->map_descriptors_
= new Map_descriptors(10);
2268 std::pair
<const Map_type
*, tree
> val(maptype
, NULL
);
2269 std::pair
<Map_descriptors::iterator
, bool> ins
=
2270 this->map_descriptors_
->insert(val
);
2271 Map_descriptors::iterator p
= ins
.first
;
2274 if (p
->second
== error_mark_node
)
2275 return error_mark_node
;
2276 gcc_assert(p
->second
!= NULL_TREE
&& DECL_P(p
->second
));
2277 return build_fold_addr_expr(p
->second
);
2280 Type
* keytype
= maptype
->key_type();
2281 Type
* valtype
= maptype
->val_type();
2283 std::string mangled_name
= ("__go_map_" + maptype
->mangled_name(this));
2285 tree id
= get_identifier_from_string(mangled_name
);
2287 // Get the type of the map descriptor. This is __go_map_descriptor
2290 tree struct_type
= this->map_descriptor_type();
2292 // The map entry type is a struct with three fields. This struct is
2293 // specific to MAPTYPE. Build it.
2295 tree map_entry_type
= make_node(RECORD_TYPE
);
2297 map_entry_type
= Gogo::builtin_struct(NULL
, "__map", map_entry_type
, 3,
2299 build_pointer_type(map_entry_type
),
2301 keytype
->get_tree(this),
2303 valtype
->get_tree(this));
2304 if (map_entry_type
== error_mark_node
)
2306 p
->second
= error_mark_node
;
2307 return error_mark_node
;
2310 tree map_entry_key_field
= DECL_CHAIN(TYPE_FIELDS(map_entry_type
));
2311 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field
)),
2314 tree map_entry_val_field
= DECL_CHAIN(map_entry_key_field
);
2315 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field
)),
2318 // Initialize the entries.
2320 tree map_descriptor_field
= TYPE_FIELDS(struct_type
);
2321 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field
)),
2322 "__map_descriptor") == 0);
2323 tree entry_size_field
= DECL_CHAIN(map_descriptor_field
);
2324 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field
)),
2325 "__entry_size") == 0);
2326 tree key_offset_field
= DECL_CHAIN(entry_size_field
);
2327 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field
)),
2328 "__key_offset") == 0);
2329 tree val_offset_field
= DECL_CHAIN(key_offset_field
);
2330 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field
)),
2331 "__val_offset") == 0);
2333 VEC(constructor_elt
, gc
)* descriptor
= VEC_alloc(constructor_elt
, gc
, 6);
2335 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2336 elt
->index
= map_descriptor_field
;
2337 elt
->value
= maptype
->type_descriptor_pointer(this);
2339 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2340 elt
->index
= entry_size_field
;
2341 elt
->value
= TYPE_SIZE_UNIT(map_entry_type
);
2343 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2344 elt
->index
= key_offset_field
;
2345 elt
->value
= byte_position(map_entry_key_field
);
2347 elt
= VEC_quick_push(constructor_elt
, descriptor
, NULL
);
2348 elt
->index
= val_offset_field
;
2349 elt
->value
= byte_position(map_entry_val_field
);
2351 tree constructor
= build_constructor(struct_type
, descriptor
);
2353 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, struct_type
);
2354 TREE_STATIC(decl
) = 1;
2355 TREE_USED(decl
) = 1;
2356 TREE_READONLY(decl
) = 1;
2357 TREE_CONSTANT(decl
) = 1;
2358 DECL_INITIAL(decl
) = constructor
;
2359 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2360 resolve_unique_section(decl
, 1, 0);
2362 rest_of_decl_compilation(decl
, 1, 0);
2364 go_preserve_from_gc(decl
);
2367 return build_fold_addr_expr(decl
);
2370 // Return a tree for the type of a map descriptor. This is struct
2371 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
2375 Gogo::map_descriptor_type()
2377 static tree struct_type
;
2378 tree dtype
= Type::make_type_descriptor_type()->get_tree(this);
2379 dtype
= build_qualified_type(dtype
, TYPE_QUAL_CONST
);
2380 return Gogo::builtin_struct(&struct_type
, "__go_map_descriptor", NULL_TREE
,
2383 build_pointer_type(dtype
),
2392 // Return the name to use for a type descriptor decl for TYPE. This
2393 // is used when TYPE does not have a name.
2396 Gogo::unnamed_type_descriptor_decl_name(const Type
* type
)
2398 return "__go_td_" + type
->mangled_name(this);
2401 // Return the name to use for a type descriptor decl for a type named
2402 // NAME, defined in the function IN_FUNCTION. IN_FUNCTION will
2403 // normally be NULL.
2406 Gogo::type_descriptor_decl_name(const Named_object
* no
,
2407 const Named_object
* in_function
)
2409 std::string ret
= "__go_tdn_";
2410 if (no
->type_value()->is_builtin())
2411 gcc_assert(in_function
== NULL
);
2414 const std::string
& unique_prefix(no
->package() == NULL
2415 ? this->unique_prefix()
2416 : no
->package()->unique_prefix());
2417 const std::string
& package_name(no
->package() == NULL
2418 ? this->package_name()
2419 : no
->package()->name());
2420 ret
.append(unique_prefix
);
2422 ret
.append(package_name
);
2424 if (in_function
!= NULL
)
2426 ret
.append(Gogo::unpack_hidden_name(in_function
->name()));
2430 ret
.append(no
->name());
2434 // Where a type descriptor decl should be defined.
2436 Gogo::Type_descriptor_location
2437 Gogo::type_descriptor_location(const Type
* type
)
2439 const Named_type
* name
= type
->named_type();
2442 if (name
->named_object()->package() != NULL
)
2444 // This is a named type defined in a different package. The
2445 // descriptor should be defined in that package.
2446 return TYPE_DESCRIPTOR_UNDEFINED
;
2448 else if (name
->is_builtin())
2450 // We create the descriptor for a builtin type whenever we
2452 return TYPE_DESCRIPTOR_COMMON
;
2456 // This is a named type defined in this package. The
2457 // descriptor should be defined here.
2458 return TYPE_DESCRIPTOR_DEFINED
;
2463 if (type
->points_to() != NULL
2464 && type
->points_to()->named_type() != NULL
2465 && type
->points_to()->named_type()->named_object()->package() != NULL
)
2467 // This is an unnamed pointer to a named type defined in a
2468 // different package. The descriptor should be defined in
2470 return TYPE_DESCRIPTOR_UNDEFINED
;
2474 // This is an unnamed type. The descriptor could be defined
2475 // in any package where it is needed, and the linker will
2476 // pick one descriptor to keep.
2477 return TYPE_DESCRIPTOR_COMMON
;
2482 // Build a type descriptor decl for TYPE. INITIALIZER is a struct
2483 // composite literal which initializers the type descriptor.
2486 Gogo::build_type_descriptor_decl(const Type
* type
, Expression
* initializer
,
2489 const Named_type
* name
= type
->named_type();
2491 // We can have multiple instances of unnamed types, but we only want
2492 // to emit the type descriptor once. We use a hash table to handle
2493 // this. This is not necessary for named types, as they are unique,
2494 // and we store the type descriptor decl in the type itself.
2498 if (this->type_descriptor_decls_
== NULL
)
2499 this->type_descriptor_decls_
= new Type_descriptor_decls(10);
2501 std::pair
<Type_descriptor_decls::iterator
, bool> ins
=
2502 this->type_descriptor_decls_
->insert(std::make_pair(type
, NULL_TREE
));
2505 // We've already built a type descriptor for this type.
2506 *pdecl
= ins
.first
->second
;
2509 phash
= &ins
.first
->second
;
2512 std::string decl_name
;
2514 decl_name
= this->unnamed_type_descriptor_decl_name(type
);
2516 decl_name
= this->type_descriptor_decl_name(name
->named_object(),
2517 name
->in_function());
2518 tree id
= get_identifier_from_string(decl_name
);
2519 tree descriptor_type_tree
= initializer
->type()->get_tree(this);
2520 if (descriptor_type_tree
== error_mark_node
)
2522 *pdecl
= error_mark_node
;
2525 tree decl
= build_decl(name
== NULL
? BUILTINS_LOCATION
: name
->location(),
2527 build_qualified_type(descriptor_type_tree
,
2529 TREE_READONLY(decl
) = 1;
2530 TREE_CONSTANT(decl
) = 1;
2531 DECL_ARTIFICIAL(decl
) = 1;
2533 go_preserve_from_gc(decl
);
2537 // We store the new DECL now because we may need to refer to it when
2538 // expanding INITIALIZER.
2541 // If appropriate, just refer to the exported type identifier.
2542 Gogo::Type_descriptor_location type_descriptor_location
=
2543 this->type_descriptor_location(type
);
2544 if (type_descriptor_location
== TYPE_DESCRIPTOR_UNDEFINED
)
2546 TREE_PUBLIC(decl
) = 1;
2547 DECL_EXTERNAL(decl
) = 1;
2551 TREE_STATIC(decl
) = 1;
2552 TREE_USED(decl
) = 1;
2554 Translate_context
context(this, NULL
, NULL
, NULL
);
2555 context
.set_is_const();
2556 tree constructor
= initializer
->get_tree(&context
);
2558 if (constructor
== error_mark_node
)
2559 gcc_assert(saw_errors());
2561 DECL_INITIAL(decl
) = constructor
;
2563 if (type_descriptor_location
== TYPE_DESCRIPTOR_DEFINED
)
2564 TREE_PUBLIC(decl
) = 1;
2567 gcc_assert(type_descriptor_location
== TYPE_DESCRIPTOR_COMMON
);
2568 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2569 resolve_unique_section(decl
, 1, 0);
2572 rest_of_decl_compilation(decl
, 1, 0);
2575 // Build an interface method table for a type: a list of function
2576 // pointers, one for each interface method. This is used for
2580 Gogo::interface_method_table_for_type(const Interface_type
* interface
,
2584 const Typed_identifier_list
* interface_methods
= interface
->methods();
2585 gcc_assert(!interface_methods
->empty());
2587 std::string mangled_name
= ((is_pointer
? "__go_pimt__" : "__go_imt_")
2588 + interface
->mangled_name(this)
2590 + type
->mangled_name(this));
2592 tree id
= get_identifier_from_string(mangled_name
);
2594 // See whether this interface has any hidden methods.
2595 bool has_hidden_methods
= false;
2596 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2597 p
!= interface_methods
->end();
2600 if (Gogo::is_hidden_name(p
->name()))
2602 has_hidden_methods
= true;
2607 // We already know that the named type is convertible to the
2608 // interface. If the interface has hidden methods, and the named
2609 // type is defined in a different package, then the interface
2610 // conversion table will be defined by that other package.
2611 if (has_hidden_methods
&& type
->named_object()->package() != NULL
)
2613 tree array_type
= build_array_type(const_ptr_type_node
, NULL
);
2614 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2615 TREE_READONLY(decl
) = 1;
2616 TREE_CONSTANT(decl
) = 1;
2617 TREE_PUBLIC(decl
) = 1;
2618 DECL_EXTERNAL(decl
) = 1;
2619 go_preserve_from_gc(decl
);
2623 size_t count
= interface_methods
->size();
2624 VEC(constructor_elt
, gc
)* pointers
= VEC_alloc(constructor_elt
, gc
,
2627 // The first element is the type descriptor.
2628 constructor_elt
* elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2629 elt
->index
= size_zero_node
;
2634 td_type
= Type::make_pointer_type(type
);
2635 elt
->value
= fold_convert(const_ptr_type_node
,
2636 td_type
->type_descriptor_pointer(this));
2639 for (Typed_identifier_list::const_iterator p
= interface_methods
->begin();
2640 p
!= interface_methods
->end();
2644 Method
* m
= type
->method_function(p
->name(), &is_ambiguous
);
2645 gcc_assert(m
!= NULL
);
2647 Named_object
* no
= m
->named_object();
2649 tree fnid
= no
->get_id(this);
2652 if (no
->is_function())
2653 fndecl
= no
->func_value()->get_or_make_decl(this, no
, fnid
);
2654 else if (no
->is_function_declaration())
2655 fndecl
= no
->func_declaration_value()->get_or_make_decl(this, no
,
2659 fndecl
= build_fold_addr_expr(fndecl
);
2661 elt
= VEC_quick_push(constructor_elt
, pointers
, NULL
);
2662 elt
->index
= size_int(i
);
2663 elt
->value
= fold_convert(const_ptr_type_node
, fndecl
);
2665 gcc_assert(i
== count
+ 1);
2667 tree array_type
= build_array_type(const_ptr_type_node
,
2668 build_index_type(size_int(count
)));
2669 tree constructor
= build_constructor(array_type
, pointers
);
2671 tree decl
= build_decl(BUILTINS_LOCATION
, VAR_DECL
, id
, array_type
);
2672 TREE_STATIC(decl
) = 1;
2673 TREE_USED(decl
) = 1;
2674 TREE_READONLY(decl
) = 1;
2675 TREE_CONSTANT(decl
) = 1;
2676 DECL_INITIAL(decl
) = constructor
;
2678 // If the interface type has hidden methods, then this is the only
2679 // definition of the table. Otherwise it is a comdat table which
2680 // may be defined in multiple packages.
2681 if (has_hidden_methods
)
2682 TREE_PUBLIC(decl
) = 1;
2685 make_decl_one_only(decl
, DECL_ASSEMBLER_NAME(decl
));
2686 resolve_unique_section(decl
, 1, 0);
2689 rest_of_decl_compilation(decl
, 1, 0);
2691 go_preserve_from_gc(decl
);
2696 // Mark a function as a builtin library function.
2699 Gogo::mark_fndecl_as_builtin_library(tree fndecl
)
2701 DECL_EXTERNAL(fndecl
) = 1;
2702 TREE_PUBLIC(fndecl
) = 1;
2703 DECL_ARTIFICIAL(fndecl
) = 1;
2704 TREE_NOTHROW(fndecl
) = 1;
2705 DECL_VISIBILITY(fndecl
) = VISIBILITY_DEFAULT
;
2706 DECL_VISIBILITY_SPECIFIED(fndecl
) = 1;
2709 // Build a call to a builtin function.
2712 Gogo::call_builtin(tree
* pdecl
, source_location location
, const char* name
,
2713 int nargs
, tree rettype
, ...)
2715 if (rettype
== error_mark_node
)
2716 return error_mark_node
;
2718 tree
* types
= new tree
[nargs
];
2719 tree
* args
= new tree
[nargs
];
2722 va_start(ap
, rettype
);
2723 for (int i
= 0; i
< nargs
; ++i
)
2725 types
[i
] = va_arg(ap
, tree
);
2726 args
[i
] = va_arg(ap
, tree
);
2727 if (types
[i
] == error_mark_node
|| args
[i
] == error_mark_node
)
2731 return error_mark_node
;
2736 if (*pdecl
== NULL_TREE
)
2738 tree fnid
= get_identifier(name
);
2740 tree argtypes
= NULL_TREE
;
2741 tree
* pp
= &argtypes
;
2742 for (int i
= 0; i
< nargs
; ++i
)
2744 *pp
= tree_cons(NULL_TREE
, types
[i
], NULL_TREE
);
2745 pp
= &TREE_CHAIN(*pp
);
2747 *pp
= void_list_node
;
2749 tree fntype
= build_function_type(rettype
, argtypes
);
2751 *pdecl
= build_decl(BUILTINS_LOCATION
, FUNCTION_DECL
, fnid
, fntype
);
2752 Gogo::mark_fndecl_as_builtin_library(*pdecl
);
2753 go_preserve_from_gc(*pdecl
);
2756 tree fnptr
= build_fold_addr_expr(*pdecl
);
2757 if (CAN_HAVE_LOCATION_P(fnptr
))
2758 SET_EXPR_LOCATION(fnptr
, location
);
2760 tree ret
= build_call_array(rettype
, fnptr
, nargs
, args
);
2761 SET_EXPR_LOCATION(ret
, location
);
2769 // Build a call to the runtime error function.
2772 Gogo::runtime_error(int code
, source_location location
)
2774 static tree runtime_error_fndecl
;
2775 tree ret
= Gogo::call_builtin(&runtime_error_fndecl
,
2777 "__go_runtime_error",
2781 build_int_cst(integer_type_node
, code
));
2782 if (ret
== error_mark_node
)
2783 return error_mark_node
;
2784 // The runtime error function panics and does not return.
2785 TREE_NOTHROW(runtime_error_fndecl
) = 0;
2786 TREE_THIS_VOLATILE(runtime_error_fndecl
) = 1;
2790 // Send VAL on CHANNEL. If BLOCKING is true, the resulting tree has a
2791 // void type. If BLOCKING is false, the resulting tree has a boolean
2792 // type, and it will evaluate as true if the value was sent. If
2793 // FOR_SELECT is true, this is being done because it was chosen in a
2794 // select statement.
2797 Gogo::send_on_channel(tree channel
, tree val
, bool blocking
, bool for_select
,
2798 source_location location
)
2800 if (channel
== error_mark_node
|| val
== error_mark_node
)
2801 return error_mark_node
;
2803 if (int_size_in_bytes(TREE_TYPE(val
)) <= 8
2804 && !AGGREGATE_TYPE_P(TREE_TYPE(val
))
2805 && !FLOAT_TYPE_P(TREE_TYPE(val
)))
2807 val
= convert_to_integer(uint64_type_node
, val
);
2810 static tree send_small_fndecl
;
2811 tree ret
= Gogo::call_builtin(&send_small_fndecl
,
2823 : boolean_false_node
));
2824 if (ret
== error_mark_node
)
2825 return error_mark_node
;
2826 // This can panic if there are too many operations on a
2828 TREE_NOTHROW(send_small_fndecl
) = 0;
2833 gcc_assert(!for_select
);
2834 static tree send_nonblocking_small_fndecl
;
2835 tree ret
= Gogo::call_builtin(&send_nonblocking_small_fndecl
,
2837 "__go_send_nonblocking_small",
2844 if (ret
== error_mark_node
)
2845 return error_mark_node
;
2846 // This can panic if there are too many operations on a
2848 TREE_NOTHROW(send_nonblocking_small_fndecl
) = 0;
2855 if (TREE_ADDRESSABLE(TREE_TYPE(val
)) || TREE_CODE(val
) == VAR_DECL
)
2857 make_tmp
= NULL_TREE
;
2858 val
= build_fold_addr_expr(val
);
2860 TREE_ADDRESSABLE(val
) = 1;
2864 tree tmp
= create_tmp_var(TREE_TYPE(val
), get_name(val
));
2865 DECL_IGNORED_P(tmp
) = 0;
2866 DECL_INITIAL(tmp
) = val
;
2867 TREE_ADDRESSABLE(tmp
) = 1;
2868 make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
2869 SET_EXPR_LOCATION(make_tmp
, location
);
2870 val
= build_fold_addr_expr(tmp
);
2872 val
= fold_convert(ptr_type_node
, val
);
2877 static tree send_big_fndecl
;
2878 call
= Gogo::call_builtin(&send_big_fndecl
,
2890 : boolean_false_node
));
2891 if (call
== error_mark_node
)
2892 return error_mark_node
;
2893 // This can panic if there are too many operations on a
2895 TREE_NOTHROW(send_big_fndecl
) = 0;
2899 gcc_assert(!for_select
);
2900 static tree send_nonblocking_big_fndecl
;
2901 call
= Gogo::call_builtin(&send_nonblocking_big_fndecl
,
2903 "__go_send_nonblocking_big",
2910 if (call
== error_mark_node
)
2911 return error_mark_node
;
2912 // This can panic if there are too many operations on a
2914 TREE_NOTHROW(send_nonblocking_big_fndecl
) = 0;
2917 if (make_tmp
== NULL_TREE
)
2921 tree ret
= build2(COMPOUND_EXPR
, TREE_TYPE(call
), make_tmp
, call
);
2922 SET_EXPR_LOCATION(ret
, location
);
2928 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
2929 // This does a blocking receive and returns the value read from the
2930 // channel. If FOR_SELECT is true, this is being done because it was
2931 // chosen in a select statement.
2934 Gogo::receive_from_channel(tree type_tree
, tree channel
, bool for_select
,
2935 source_location location
)
2937 if (type_tree
== error_mark_node
|| channel
== error_mark_node
)
2938 return error_mark_node
;
2940 if (int_size_in_bytes(type_tree
) <= 8
2941 && !AGGREGATE_TYPE_P(type_tree
)
2942 && !FLOAT_TYPE_P(type_tree
))
2944 static tree receive_small_fndecl
;
2945 tree call
= Gogo::call_builtin(&receive_small_fndecl
,
2947 "__go_receive_small",
2955 : boolean_false_node
));
2956 if (call
== error_mark_node
)
2957 return error_mark_node
;
2958 // This can panic if there are too many operations on a closed
2960 TREE_NOTHROW(receive_small_fndecl
) = 0;
2961 int bitsize
= GET_MODE_BITSIZE(TYPE_MODE(type_tree
));
2962 tree int_type_tree
= go_type_for_size(bitsize
, 1);
2963 return fold_convert_loc(location
, type_tree
,
2964 fold_convert_loc(location
, int_type_tree
,
2969 tree tmp
= create_tmp_var(type_tree
, get_name(type_tree
));
2970 DECL_IGNORED_P(tmp
) = 0;
2971 TREE_ADDRESSABLE(tmp
) = 1;
2972 tree make_tmp
= build1(DECL_EXPR
, void_type_node
, tmp
);
2973 SET_EXPR_LOCATION(make_tmp
, location
);
2974 tree tmpaddr
= build_fold_addr_expr(tmp
);
2975 tmpaddr
= fold_convert(ptr_type_node
, tmpaddr
);
2976 static tree receive_big_fndecl
;
2977 tree call
= Gogo::call_builtin(&receive_big_fndecl
,
2989 : boolean_false_node
));
2990 if (call
== error_mark_node
)
2991 return error_mark_node
;
2992 // This can panic if there are too many operations on a closed
2994 TREE_NOTHROW(receive_big_fndecl
) = 0;
2995 return build2(COMPOUND_EXPR
, type_tree
, make_tmp
,
2996 build2(COMPOUND_EXPR
, type_tree
, call
, tmp
));
3000 // Return the type of a function trampoline. This is like
3001 // get_trampoline_type in tree-nested.c.
3004 Gogo::trampoline_type_tree()
3006 static tree type_tree
;
3007 if (type_tree
== NULL_TREE
)
3011 go_trampoline_info(&size
, &align
);
3012 tree t
= build_index_type(build_int_cst(integer_type_node
, size
- 1));
3013 t
= build_array_type(char_type_node
, t
);
3015 type_tree
= Gogo::builtin_struct(NULL
, "__go_trampoline", NULL_TREE
, 1,
3017 t
= TYPE_FIELDS(type_tree
);
3018 DECL_ALIGN(t
) = align
;
3019 DECL_USER_ALIGN(t
) = 1;
3021 go_preserve_from_gc(type_tree
);
3026 // Make a trampoline which calls FNADDR passing CLOSURE.
3029 Gogo::make_trampoline(tree fnaddr
, tree closure
, source_location location
)
3031 tree trampoline_type
= Gogo::trampoline_type_tree();
3032 tree trampoline_size
= TYPE_SIZE_UNIT(trampoline_type
);
3034 closure
= save_expr(closure
);
3036 // We allocate the trampoline using a special function which will
3037 // mark it as executable.
3038 static tree trampoline_fndecl
;
3039 tree x
= Gogo::call_builtin(&trampoline_fndecl
,
3041 "__go_allocate_trampoline",
3047 fold_convert_loc(location
, ptr_type_node
,
3049 if (x
== error_mark_node
)
3050 return error_mark_node
;
3054 // Initialize the trampoline.
3055 tree ini
= build_call_expr(implicit_built_in_decls
[BUILT_IN_INIT_TRAMPOLINE
],
3056 3, x
, fnaddr
, closure
);
3058 // On some targets the trampoline address needs to be adjusted. For
3059 // example, when compiling in Thumb mode on the ARM, the address
3060 // needs to have the low bit set.
3061 x
= build_call_expr(implicit_built_in_decls
[BUILT_IN_ADJUST_TRAMPOLINE
],
3063 x
= fold_convert(TREE_TYPE(fnaddr
), x
);
3065 return build2(COMPOUND_EXPR
, TREE_TYPE(x
), ini
, x
);