ptr_traits.h (pointer_traits): Fix typos.
[gcc.git] / gcc / go / gofrontend / gogo-tree.cc.working
1 // gogo-tree.cc -- convert Go frontend Gogo IR to gcc trees.
2
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.
6
7 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "toplev.h"
17 #include "tree.h"
18 #include "gimple.h"
19 #include "tree-iterator.h"
20 #include "cgraph.h"
21 #include "langhooks.h"
22 #include "convert.h"
23 #include "output.h"
24 #include "diagnostic.h"
25
26 #ifndef ENABLE_BUILD_WITH_CXX
27 }
28 #endif
29
30 #include "go-c.h"
31 #include "types.h"
32 #include "expressions.h"
33 #include "statements.h"
34 #include "gogo.h"
35
36 // Whether we have seen any errors.
37
38 bool
39 saw_errors()
40 {
41 return errorcount != 0 || sorrycount != 0;
42 }
43
44 // A helper function.
45
46 static inline tree
47 get_identifier_from_string(const std::string& str)
48 {
49 return get_identifier_with_length(str.data(), str.length());
50 }
51
52 // Builtin functions.
53
54 static std::map<std::string, tree> builtin_functions;
55
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.
61
62 static void
63 define_builtin(built_in_function bcode, const char* name, const char* libname,
64 tree fntype, bool const_p)
65 {
66 tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
67 libname, NULL_TREE);
68 if (const_p)
69 TREE_READONLY(decl) = 1;
70 built_in_decls[bcode] = decl;
71 implicit_built_in_decls[bcode] = decl;
72 builtin_functions[name] = decl;
73 if (libname != NULL)
74 {
75 decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
76 NULL, NULL_TREE);
77 if (const_p)
78 TREE_READONLY(decl) = 1;
79 builtin_functions[libname] = decl;
80 }
81 }
82
83 // Create trees for implicit builtin functions.
84
85 void
86 Gogo::define_builtin_function_trees()
87 {
88 /* We need to define the fetch_and_add functions, since we use them
89 for ++ and --. */
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);
94
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);
99
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);
104
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);
109
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,
115 NULL_TREE),
116 true);
117
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,
121 ptr_type_node,
122 const_ptr_type_node,
123 size_type_node,
124 NULL_TREE),
125 false);
126
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,
130 double_type_node,
131 NULL_TREE),
132 true);
133 define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
134 build_function_type_list(long_double_type_node,
135 long_double_type_node,
136 NULL_TREE),
137 true);
138
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,
143 unsigned_type_node,
144 NULL_TREE),
145 false);
146
147 // The compiler uses __builtin_trap for some exception handling
148 // cases.
149 define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
150 build_function_type(void_type_node, void_list_node),
151 false);
152 }
153
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.
157
158 const std::string&
159 Gogo::get_init_fn_name()
160 {
161 if (this->init_fn_name_.empty())
162 {
163 gcc_assert(this->package_ != NULL);
164 if (this->is_main_package())
165 {
166 // Use a name which the runtime knows.
167 this->init_fn_name_ = "__go_init_main";
168 }
169 else
170 {
171 std::string s = this->unique_prefix();
172 s.append(1, '.');
173 s.append(this->package_name());
174 s.append("..import");
175 this->init_fn_name_ = s;
176 }
177 }
178
179 return this->init_fn_name_;
180 }
181
182 // Add statements to INIT_STMT_LIST which run the initialization
183 // functions for imported packages. This is only used for the "main"
184 // package.
185
186 void
187 Gogo::init_imports(tree* init_stmt_list)
188 {
189 gcc_assert(this->is_main_package());
190
191 if (this->imported_init_fns_.empty())
192 return;
193
194 tree fntype = build_function_type(void_type_node, void_list_node);
195
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();
201 ++p)
202 v.push_back(*p);
203 std::sort(v.begin(), v.end());
204
205 for (std::vector<Import_init>::const_iterator p = v.begin();
206 p != v.end();
207 ++p)
208 {
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),
212 fntype);
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);
218 }
219 }
220
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.
225
226 // struct __go_gc_root_list
227 // {
228 // struct __go_gc_root_list* __next;
229 // struct __go_gc_root
230 // {
231 // void* __decl;
232 // size_t __size;
233 // } __roots[];
234 // };
235
236 // The last entry in the roots array has a NULL decl field.
237
238 void
239 Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
240 tree* init_stmt_list)
241 {
242 if (var_gc.empty())
243 return;
244
245 size_t count = var_gc.size();
246
247 tree root_type = Gogo::builtin_struct(NULL, "__go_gc_root", NULL_TREE, 2,
248 "__next",
249 ptr_type_node,
250 "__size",
251 sizetype);
252
253 tree index_type = build_index_type(size_int(count));
254 tree array_type = build_array_type(root_type, index_type);
255
256 tree root_list_type = make_node(RECORD_TYPE);
257 root_list_type = Gogo::builtin_struct(NULL, "__go_gc_root_list",
258 root_list_type, 2,
259 "__next",
260 build_pointer_type(root_list_type),
261 "__roots",
262 array_type);
263
264 // Build an initialier for the __roots array.
265
266 VEC(constructor_elt,gc)* roots_init = VEC_alloc(constructor_elt, gc,
267 count + 1);
268
269 size_t i = 0;
270 for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
271 p != var_gc.end();
272 ++p, ++i)
273 {
274 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
275
276 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
277 tree field = TYPE_FIELDS(root_type);
278 elt->index = field;
279 tree decl = (*p)->get_tree(this, NULL);
280 gcc_assert(TREE_CODE(decl) == VAR_DECL);
281 elt->value = build_fold_addr_expr(decl);
282
283 elt = VEC_quick_push(constructor_elt, init, NULL);
284 field = DECL_CHAIN(field);
285 elt->index = field;
286 elt->value = DECL_SIZE_UNIT(decl);
287
288 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
289 elt->index = size_int(i);
290 elt->value = build_constructor(root_type, init);
291 }
292
293 // The list ends with a NULL entry.
294
295 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
296
297 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
298 tree field = TYPE_FIELDS(root_type);
299 elt->index = field;
300 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
301
302 elt = VEC_quick_push(constructor_elt, init, NULL);
303 field = DECL_CHAIN(field);
304 elt->index = field;
305 elt->value = size_zero_node;
306
307 elt = VEC_quick_push(constructor_elt, roots_init, NULL);
308 elt->index = size_int(i);
309 elt->value = build_constructor(root_type, init);
310
311 // Build a constructor for the struct.
312
313 VEC(constructor_elt,gc*) root_list_init = VEC_alloc(constructor_elt, gc, 2);
314
315 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
316 field = TYPE_FIELDS(root_list_type);
317 elt->index = field;
318 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
319
320 elt = VEC_quick_push(constructor_elt, root_list_init, NULL);
321 field = DECL_CHAIN(field);
322 elt->index = field;
323 elt->value = build_constructor(array_type, roots_init);
324
325 // Build a decl to register.
326
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);
335
336 static tree register_gc_fndecl;
337 tree call = Gogo::call_builtin(&register_gc_fndecl, BUILTINS_LOCATION,
338 "__go_register_gc_roots",
339 1,
340 void_type_node,
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);
345 }
346
347 // Build the decl for the initialization function.
348
349 tree
350 Gogo::initialization_function_decl()
351 {
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,
358 void_list_node));
359 const std::string& asm_name(this->get_init_fn_name());
360 SET_DECL_ASSEMBLER_NAME(fndecl, get_identifier_from_string(asm_name));
361
362 tree resdecl = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE,
363 void_type_node);
364 DECL_ARTIFICIAL(resdecl) = 1;
365 DECL_CONTEXT(resdecl) = fndecl;
366 DECL_RESULT(fndecl) = resdecl;
367
368 TREE_STATIC(fndecl) = 1;
369 TREE_USED(fndecl) = 1;
370 DECL_ARTIFICIAL(fndecl) = 1;
371 TREE_PUBLIC(fndecl) = 1;
372
373 DECL_INITIAL(fndecl) = make_node(BLOCK);
374 TREE_USED(DECL_INITIAL(fndecl)) = 1;
375
376 return fndecl;
377 }
378
379 // Create the magic initialization function. INIT_STMT_LIST is the
380 // code that it needs to run.
381
382 void
383 Gogo::write_initialization_function(tree fndecl, tree init_stmt_list)
384 {
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_);
388
389 if (fndecl == NULL_TREE)
390 fndecl = this->initialization_function_decl();
391
392 DECL_SAVED_TREE(fndecl) = init_stmt_list;
393
394 current_function_decl = fndecl;
395 if (DECL_STRUCT_FUNCTION(fndecl) == NULL)
396 push_struct_function(fndecl);
397 else
398 push_cfun(DECL_STRUCT_FUNCTION(fndecl));
399 cfun->function_end_locus = BUILTINS_LOCATION;
400
401 gimplify_function_tree(fndecl);
402
403 cgraph_add_new_function(fndecl, false);
404 cgraph_mark_needed_node(cgraph_node(fndecl));
405
406 current_function_decl = NULL_TREE;
407 pop_cfun();
408 }
409
410 // Search for references to VAR in any statements or called functions.
411
412 class Find_var : public Traverse
413 {
414 public:
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
417 // package.
418 typedef Unordered_set(std::string) Seen_objects;
419
420 Find_var(Named_object* var, Seen_objects* seen_objects)
421 : Traverse(traverse_expressions),
422 var_(var), seen_objects_(seen_objects), found_(false)
423 { }
424
425 // Whether the variable was found.
426 bool
427 found() const
428 { return this->found_; }
429
430 int
431 expression(Expression**);
432
433 private:
434 // The variable we are looking for.
435 Named_object* var_;
436 // Names of objects we have already seen.
437 Seen_objects* seen_objects_;
438 // True if the variable was found.
439 bool found_;
440 };
441
442 // See if EXPR refers to VAR, looking through function calls and
443 // variable initializations.
444
445 int
446 Find_var::expression(Expression** pexpr)
447 {
448 Expression* e = *pexpr;
449
450 Var_expression* ve = e->var_expression();
451 if (ve != NULL)
452 {
453 Named_object* v = ve->named_object();
454 if (v == this->var_)
455 {
456 this->found_ = true;
457 return TRAVERSE_EXIT;
458 }
459
460 if (v->is_variable() && v->package() == NULL)
461 {
462 Expression* init = v->var_value()->init();
463 if (init != NULL)
464 {
465 std::pair<Seen_objects::iterator, bool> ins =
466 this->seen_objects_->insert(v->name());
467 if (ins.second)
468 {
469 // This is the first time we have seen this name.
470 if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
471 return TRAVERSE_EXIT;
472 }
473 }
474 }
475 }
476
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();
481 if (fe != NULL)
482 {
483 const Named_object* f = fe->named_object();
484 if (f->is_function() && f->package() == NULL)
485 {
486 std::pair<Seen_objects::iterator, bool> ins =
487 this->seen_objects_->insert(f->name());
488 if (ins.second)
489 {
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;
493 }
494 }
495 }
496
497 return TRAVERSE_CONTINUE;
498 }
499
500 // Return true if EXPR refers to VAR.
501
502 static bool
503 expression_requires(Expression* expr, Block* preinit, Named_object* var)
504 {
505 Find_var::Seen_objects seen_objects;
506 Find_var find_var(var, &seen_objects);
507 if (expr != NULL)
508 Expression::traverse(&expr, &find_var);
509 if (preinit != NULL)
510 preinit->traverse(&find_var);
511
512 return find_var.found();
513 }
514
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.
518
519 class Var_init
520 {
521 public:
522 Var_init()
523 : var_(NULL), init_(NULL_TREE), waiting_(0)
524 { }
525
526 Var_init(Named_object* var, tree init)
527 : var_(var), init_(init), waiting_(0)
528 { }
529
530 // Return the variable.
531 Named_object*
532 var() const
533 { return this->var_; }
534
535 // Return the initialization expression.
536 tree
537 init() const
538 { return this->init_; }
539
540 // Return the number of variables waiting for this one to be
541 // initialized.
542 size_t
543 waiting() const
544 { return this->waiting_; }
545
546 // Increment the number waiting.
547 void
548 increment_waiting()
549 { ++this->waiting_; }
550
551 private:
552 // The variable being initialized.
553 Named_object* var_;
554 // The initialization expression to run.
555 tree init_;
556 // The number of variables which are waiting for this one.
557 size_t waiting_;
558 };
559
560 typedef std::list<Var_init> Var_inits;
561
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.
566
567 static void
568 sort_var_inits(Var_inits* var_inits)
569 {
570 Var_inits ready;
571 while (!var_inits->empty())
572 {
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();
577
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;
582 ++p2;
583 for (size_t i = p1->waiting(); i > 0; --i)
584 ++p2;
585
586 for (; p2 != var_inits->end(); ++p2)
587 {
588 if (expression_requires(init, preinit, p2->var()))
589 {
590 // Check for cycles.
591 if (expression_requires(p2->var()->var_value()->init(),
592 p2->var()->var_value()->preinit(),
593 var))
594 {
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();
603 }
604 else
605 {
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)
612 ++p3;
613 var_inits->splice(p3, *var_inits, p1);
614 }
615 break;
616 }
617 }
618
619 if (p2 == var_inits->end())
620 {
621 // VAR does not depends upon any other initialization expressions.
622
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);
631 }
632 }
633
634 // Now READY is the list in the desired initialization order.
635 var_inits->swap(ready);
636 }
637
638 // Write out the global definitions.
639
640 void
641 Gogo::write_globals()
642 {
643 this->convert_named_types();
644 this->build_interface_method_tables();
645
646 Bindings* bindings = this->current_bindings();
647 size_t count = bindings->size_definitions();
648
649 tree* vec = new tree[count];
650
651 tree init_fndecl = NULL_TREE;
652 tree init_stmt_list = NULL_TREE;
653
654 if (this->is_main_package())
655 this->init_imports(&init_stmt_list);
656
657 // A list of variable initializations.
658 Var_inits var_inits;
659
660 // A list of variables which need to be registered with the garbage
661 // collector.
662 std::vector<Named_object*> var_gc;
663 var_gc.reserve(count);
664
665 tree var_init_stmt_list = NULL_TREE;
666 size_t i = 0;
667 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
668 p != bindings->end_definitions();
669 ++p, ++i)
670 {
671 Named_object* no = *p;
672
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())
676 {
677 --i;
678 --count;
679 continue;
680 }
681
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)
685 {
686 --i;
687 --count;
688 continue;
689 }
690
691 // There is nothing useful we can output for constants which
692 // have ideal or non-integeral type.
693 if (no->is_const())
694 {
695 Type* type = no->const_value()->type();
696 if (type == NULL)
697 type = no->const_value()->expr()->type();
698 if (type->is_abstract() || type->integer_type() == NULL)
699 {
700 --i;
701 --count;
702 continue;
703 }
704 }
705
706 vec[i] = no->get_tree(this, NULL);
707
708 if (vec[i] == error_mark_node)
709 {
710 gcc_assert(saw_errors());
711 --i;
712 --count;
713 continue;
714 }
715
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)
719 {
720 gcc_assert(no->is_variable());
721
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] == '.';
725
726 tree var_init_tree = NULL_TREE;
727 if (!no->var_value()->has_pre_init())
728 {
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)
733 ;
734 else if (TREE_CONSTANT(init))
735 DECL_INITIAL(vec[i]) = init;
736 else if (is_sink)
737 var_init_tree = init;
738 else
739 var_init_tree = fold_build2_loc(no->location(), MODIFY_EXPR,
740 void_type_node, vec[i], init);
741 }
742 else
743 {
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);
751 else
752 push_cfun(DECL_STRUCT_FUNCTION(init_fndecl));
753
754 tree var_decl = is_sink ? NULL_TREE : vec[i];
755 var_init_tree = no->var_value()->get_init_block(this, NULL,
756 var_decl);
757
758 current_function_decl = NULL_TREE;
759 pop_cfun();
760 }
761
762 if (var_init_tree != NULL_TREE && var_init_tree != error_mark_node)
763 {
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);
767 else
768 var_inits.push_back(Var_init(no, var_init_tree));
769 }
770
771 if (!is_sink && no->var_value()->type()->has_pointer())
772 var_gc.push_back(no);
773 }
774 }
775
776 // Register global variables with the garbage collector.
777 this->register_gc_vars(var_gc, &init_stmt_list);
778
779 // Simple variable initializations, after all variables are
780 // registered.
781 append_to_statement_list(var_init_stmt_list, &init_stmt_list);
782
783 // Complex variable initializations, first sorting them into a
784 // workable order.
785 if (!var_inits.empty())
786 {
787 sort_var_inits(&var_inits);
788 for (Var_inits::const_iterator p = var_inits.begin();
789 p != var_inits.end();
790 ++p)
791 append_to_statement_list(p->init(), &init_stmt_list);
792 }
793
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();
799 ++p)
800 {
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);
804 }
805
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);
812
813 // Pass everything back to the middle-end.
814
815 wrapup_global_declarations(vec, count);
816
817 cgraph_finalize_compilation_unit();
818
819 check_global_declarations(vec, count);
820 emit_debug_global_declarations(vec, count);
821
822 delete[] vec;
823 }
824
825 // Get a tree for the identifier for a named object.
826
827 tree
828 Named_object::get_id(Gogo* gogo)
829 {
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())
835 || (this->is_type()
836 && this->type_value()->location() == BUILTINS_LOCATION))
837 {
838 // We don't need the package name for local variables or builtin
839 // types.
840 decl_name = Gogo::unpack_hidden_name(this->name_);
841 }
842 else
843 {
844 std::string package_name;
845 if (this->package_ == NULL)
846 package_name = gogo->package_name();
847 else
848 package_name = this->package_->name();
849
850 decl_name = package_name + '.' + Gogo::unpack_hidden_name(this->name_);
851
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();
857 else
858 fntype = NULL;
859 if (fntype != NULL && fntype->is_method())
860 {
861 decl_name.push_back('.');
862 decl_name.append(fntype->receiver()->type()->mangled_name(gogo));
863 }
864 }
865 if (this->is_type())
866 {
867 const Named_object* in_function = this->type_value()->in_function();
868 if (in_function != NULL)
869 decl_name += '$' + in_function->name();
870 }
871 return get_identifier_from_string(decl_name);
872 }
873
874 // Get a tree for a named object.
875
876 tree
877 Named_object::get_tree(Gogo* gogo, Named_object* function)
878 {
879 if (this->tree_ != NULL_TREE)
880 {
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)
889 {
890 gcc_assert(TREE_CODE(ret) == INDIRECT_REF);
891 ret = build_fold_indirect_ref(TREE_OPERAND(ret, 0));
892 TREE_THIS_NOTRAP(ret) = 1;
893 }
894 return ret;
895 }
896
897 tree name;
898 if (this->classification_ == NAMED_OBJECT_TYPE)
899 name = NULL_TREE;
900 else
901 name = this->get_id(gogo);
902 tree decl;
903 switch (this->classification_)
904 {
905 case NAMED_OBJECT_CONST:
906 {
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;
912 else
913 {
914 Type* type = named_constant->type();
915 if (type != NULL && !type->is_abstract())
916 {
917 if (!type->is_undefined())
918 expr_tree = fold_convert(type->get_tree(gogo), expr_tree);
919 else
920 {
921 // Make sure we report the error.
922 type->base();
923 expr_tree = error_mark_node;
924 }
925 }
926 if (expr_tree == error_mark_node)
927 decl = error_mark_node;
928 else if (INTEGRAL_TYPE_P(TREE_TYPE(expr_tree)))
929 {
930 decl = build_decl(named_constant->location(), CONST_DECL,
931 name, TREE_TYPE(expr_tree));
932 DECL_INITIAL(decl) = expr_tree;
933 TREE_CONSTANT(decl) = 1;
934 TREE_READONLY(decl) = 1;
935 }
936 else
937 {
938 // A CONST_DECL is only for an enum constant, so we
939 // shouldn't use for non-integral types. Instead we
940 // just return the constant itself, rather than a
941 // decl.
942 decl = expr_tree;
943 }
944 }
945 }
946 break;
947
948 case NAMED_OBJECT_TYPE:
949 {
950 Named_type* named_type = this->u_.type_value;
951 tree type_tree = named_type->get_tree(gogo);
952 if (type_tree == error_mark_node)
953 decl = error_mark_node;
954 else
955 {
956 decl = TYPE_NAME(type_tree);
957 gcc_assert(decl != NULL_TREE);
958
959 // We need to produce a type descriptor for every named
960 // type, and for a pointer to every named type, since
961 // other files or packages might refer to them. We need
962 // to do this even for hidden types, because they might
963 // still be returned by some function. Simply calling the
964 // type_descriptor method is enough to create the type
965 // descriptor, even though we don't do anything with it.
966 if (this->package_ == NULL)
967 {
968 named_type->type_descriptor_pointer(gogo);
969 Type* pn = Type::make_pointer_type(named_type);
970 pn->type_descriptor_pointer(gogo);
971 }
972 }
973 }
974 break;
975
976 case NAMED_OBJECT_TYPE_DECLARATION:
977 error("reference to undefined type %qs",
978 this->message_name().c_str());
979 return error_mark_node;
980
981 case NAMED_OBJECT_VAR:
982 {
983 Variable* var = this->u_.var_value;
984 Type* type = var->type();
985 if (type->is_error_type()
986 || (type->is_undefined()
987 && (!var->is_global() || this->package() == NULL)))
988 {
989 // Force the error for an undefined type, just in case.
990 type->base();
991 decl = error_mark_node;
992 }
993 else
994 {
995 tree var_type = type->get_tree(gogo);
996 bool is_parameter = var->is_parameter();
997 if (var->is_receiver() && type->points_to() == NULL)
998 is_parameter = false;
999 if (var->is_in_heap())
1000 {
1001 is_parameter = false;
1002 var_type = build_pointer_type(var_type);
1003 }
1004 decl = build_decl(var->location(),
1005 is_parameter ? PARM_DECL : VAR_DECL,
1006 name, var_type);
1007 if (!var->is_global())
1008 {
1009 tree fnid = function->get_id(gogo);
1010 tree fndecl = function->func_value()->get_or_make_decl(gogo,
1011 function,
1012 fnid);
1013 DECL_CONTEXT(decl) = fndecl;
1014 }
1015 if (is_parameter)
1016 DECL_ARG_TYPE(decl) = TREE_TYPE(decl);
1017
1018 if (var->is_global())
1019 {
1020 const Package* package = this->package();
1021 if (package == NULL)
1022 TREE_STATIC(decl) = 1;
1023 else
1024 DECL_EXTERNAL(decl) = 1;
1025 if (!Gogo::is_hidden_name(this->name_))
1026 {
1027 TREE_PUBLIC(decl) = 1;
1028 std::string asm_name = (package == NULL
1029 ? gogo->unique_prefix()
1030 : package->unique_prefix());
1031 asm_name.append(1, '.');
1032 asm_name.append(IDENTIFIER_POINTER(name),
1033 IDENTIFIER_LENGTH(name));
1034 tree asm_id = get_identifier_from_string(asm_name);
1035 SET_DECL_ASSEMBLER_NAME(decl, asm_id);
1036 }
1037 }
1038
1039 // FIXME: We should only set this for variables which are
1040 // actually used somewhere.
1041 TREE_USED(decl) = 1;
1042 }
1043 }
1044 break;
1045
1046 case NAMED_OBJECT_RESULT_VAR:
1047 {
1048 Result_variable* result = this->u_.result_var_value;
1049 Type* type = result->type();
1050 if (type->is_error_type() || type->is_undefined())
1051 {
1052 // Force the error.
1053 type->base();
1054 decl = error_mark_node;
1055 }
1056 else
1057 {
1058 gcc_assert(result->function() == function->func_value());
1059 source_location loc = function->location();
1060 tree result_type = type->get_tree(gogo);
1061 tree init;
1062 if (!result->is_in_heap())
1063 init = type->get_init_tree(gogo, false);
1064 else
1065 {
1066 tree space = gogo->allocate_memory(type,
1067 TYPE_SIZE_UNIT(result_type),
1068 loc);
1069 result_type = build_pointer_type(result_type);
1070 tree subinit = type->get_init_tree(gogo, true);
1071 if (subinit == NULL_TREE)
1072 init = fold_convert_loc(loc, result_type, space);
1073 else
1074 {
1075 space = save_expr(space);
1076 space = fold_convert_loc(loc, result_type, space);
1077 tree spaceref = build_fold_indirect_ref_loc(loc, space);
1078 TREE_THIS_NOTRAP(spaceref) = 1;
1079 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1080 spaceref, subinit);
1081 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1082 set, space);
1083 }
1084 }
1085 decl = build_decl(loc, VAR_DECL, name, result_type);
1086 tree fnid = function->get_id(gogo);
1087 tree fndecl = function->func_value()->get_or_make_decl(gogo,
1088 function,
1089 fnid);
1090 DECL_CONTEXT(decl) = fndecl;
1091 DECL_INITIAL(decl) = init;
1092 TREE_USED(decl) = 1;
1093 }
1094 }
1095 break;
1096
1097 case NAMED_OBJECT_SINK:
1098 gcc_unreachable();
1099
1100 case NAMED_OBJECT_FUNC:
1101 {
1102 Function* func = this->u_.func_value;
1103 decl = func->get_or_make_decl(gogo, this, name);
1104 if (decl != error_mark_node)
1105 {
1106 if (func->block() != NULL)
1107 {
1108 if (DECL_STRUCT_FUNCTION(decl) == NULL)
1109 push_struct_function(decl);
1110 else
1111 push_cfun(DECL_STRUCT_FUNCTION(decl));
1112
1113 cfun->function_end_locus = func->block()->end_location();
1114
1115 current_function_decl = decl;
1116
1117 func->build_tree(gogo, this);
1118
1119 gimplify_function_tree(decl);
1120
1121 cgraph_finalize_function(decl, true);
1122
1123 current_function_decl = NULL_TREE;
1124 pop_cfun();
1125 }
1126 }
1127 }
1128 break;
1129
1130 default:
1131 gcc_unreachable();
1132 }
1133
1134 if (TREE_TYPE(decl) == error_mark_node)
1135 decl = error_mark_node;
1136
1137 tree ret = decl;
1138
1139 // If this is a local variable whose address is taken, then we
1140 // actually store it in the heap. For uses of the variable we need
1141 // to return a reference to that heap location.
1142 if (((this->classification_ == NAMED_OBJECT_VAR
1143 && this->var_value()->is_in_heap())
1144 || (this->classification_ == NAMED_OBJECT_RESULT_VAR
1145 && this->result_var_value()->is_in_heap()))
1146 && ret != error_mark_node)
1147 {
1148 gcc_assert(POINTER_TYPE_P(TREE_TYPE(ret)));
1149 ret = build_fold_indirect_ref(ret);
1150 TREE_THIS_NOTRAP(ret) = 1;
1151 }
1152
1153 this->tree_ = ret;
1154
1155 if (ret != error_mark_node)
1156 go_preserve_from_gc(ret);
1157
1158 return ret;
1159 }
1160
1161 // Get the initial value of a variable as a tree. This does not
1162 // consider whether the variable is in the heap--it returns the
1163 // initial value as though it were always stored in the stack.
1164
1165 tree
1166 Variable::get_init_tree(Gogo* gogo, Named_object* function)
1167 {
1168 gcc_assert(this->preinit_ == NULL);
1169 if (this->init_ == NULL)
1170 {
1171 gcc_assert(!this->is_parameter_);
1172 return this->type_->get_init_tree(gogo, this->is_global_);
1173 }
1174 else
1175 {
1176 Translate_context context(gogo, function, NULL, NULL_TREE);
1177 tree rhs_tree = this->init_->get_tree(&context);
1178 return Expression::convert_for_assignment(&context, this->type(),
1179 this->init_->type(),
1180 rhs_tree, this->location());
1181 }
1182 }
1183
1184 // Get the initial value of a variable when a block is required.
1185 // VAR_DECL is the decl to set; it may be NULL for a sink variable.
1186
1187 tree
1188 Variable::get_init_block(Gogo* gogo, Named_object* function, tree var_decl)
1189 {
1190 gcc_assert(this->preinit_ != NULL);
1191
1192 // We want to add the variable assignment to the end of the preinit
1193 // block. The preinit block may have a TRY_FINALLY_EXPR and a
1194 // TRY_CATCH_EXPR; if it does, we want to add to the end of the
1195 // regular statements.
1196
1197 Translate_context context(gogo, function, NULL, NULL_TREE);
1198 tree block_tree = this->preinit_->get_tree(&context);
1199 if (block_tree == error_mark_node)
1200 return error_mark_node;
1201 gcc_assert(TREE_CODE(block_tree) == BIND_EXPR);
1202 tree statements = BIND_EXPR_BODY(block_tree);
1203 while (statements != NULL_TREE
1204 && (TREE_CODE(statements) == TRY_FINALLY_EXPR
1205 || TREE_CODE(statements) == TRY_CATCH_EXPR))
1206 statements = TREE_OPERAND(statements, 0);
1207
1208 // It's possible to have pre-init statements without an initializer
1209 // if the pre-init statements set the variable.
1210 if (this->init_ != NULL)
1211 {
1212 tree rhs_tree = this->init_->get_tree(&context);
1213 if (rhs_tree == error_mark_node)
1214 return error_mark_node;
1215 if (var_decl == NULL_TREE)
1216 append_to_statement_list(rhs_tree, &statements);
1217 else
1218 {
1219 tree val = Expression::convert_for_assignment(&context, this->type(),
1220 this->init_->type(),
1221 rhs_tree,
1222 this->location());
1223 if (val == error_mark_node)
1224 return error_mark_node;
1225 tree set = fold_build2_loc(this->location(), MODIFY_EXPR,
1226 void_type_node, var_decl, val);
1227 append_to_statement_list(set, &statements);
1228 }
1229 }
1230
1231 return block_tree;
1232 }
1233
1234 // Get a tree for a function decl.
1235
1236 tree
1237 Function::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1238 {
1239 if (this->fndecl_ == NULL_TREE)
1240 {
1241 tree functype = this->type_->get_tree(gogo);
1242 if (functype == error_mark_node)
1243 this->fndecl_ = error_mark_node;
1244 else
1245 {
1246 // The type of a function comes back as a pointer, but we
1247 // want the real function type for a function declaration.
1248 gcc_assert(POINTER_TYPE_P(functype));
1249 functype = TREE_TYPE(functype);
1250 tree decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1251
1252 this->fndecl_ = decl;
1253
1254 if (no->package() != NULL)
1255 ;
1256 else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
1257 ;
1258 else if (Gogo::unpack_hidden_name(no->name()) == "init"
1259 && !this->type_->is_method())
1260 ;
1261 else if (Gogo::unpack_hidden_name(no->name()) == "main"
1262 && gogo->is_main_package())
1263 TREE_PUBLIC(decl) = 1;
1264 // Methods have to be public even if they are hidden because
1265 // they can be pulled into type descriptors when using
1266 // anonymous fields.
1267 else if (!Gogo::is_hidden_name(no->name())
1268 || this->type_->is_method())
1269 {
1270 TREE_PUBLIC(decl) = 1;
1271 std::string asm_name = gogo->unique_prefix();
1272 asm_name.append(1, '.');
1273 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1274 SET_DECL_ASSEMBLER_NAME(decl,
1275 get_identifier_from_string(asm_name));
1276 }
1277
1278 // Why do we have to do this in the frontend?
1279 tree restype = TREE_TYPE(functype);
1280 tree resdecl = build_decl(this->location(), RESULT_DECL, NULL_TREE,
1281 restype);
1282 DECL_ARTIFICIAL(resdecl) = 1;
1283 DECL_IGNORED_P(resdecl) = 1;
1284 DECL_CONTEXT(resdecl) = decl;
1285 DECL_RESULT(decl) = resdecl;
1286
1287 if (this->enclosing_ != NULL)
1288 DECL_STATIC_CHAIN(decl) = 1;
1289
1290 // If a function calls the predeclared recover function, we
1291 // can't inline it, because recover behaves differently in a
1292 // function passed directly to defer.
1293 if (this->calls_recover_ && !this->is_recover_thunk_)
1294 DECL_UNINLINABLE(decl) = 1;
1295
1296 // If this is a thunk created to call a function which calls
1297 // the predeclared recover function, we need to disable
1298 // stack splitting for the thunk.
1299 if (this->is_recover_thunk_)
1300 {
1301 tree attr = get_identifier("__no_split_stack__");
1302 DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
1303 }
1304
1305 go_preserve_from_gc(decl);
1306
1307 if (this->closure_var_ != NULL)
1308 {
1309 push_struct_function(decl);
1310
1311 tree closure_decl = this->closure_var_->get_tree(gogo, no);
1312 if (closure_decl == error_mark_node)
1313 this->fndecl_ = error_mark_node;
1314 else
1315 {
1316 DECL_ARTIFICIAL(closure_decl) = 1;
1317 DECL_IGNORED_P(closure_decl) = 1;
1318 TREE_USED(closure_decl) = 1;
1319 DECL_ARG_TYPE(closure_decl) = TREE_TYPE(closure_decl);
1320 TREE_READONLY(closure_decl) = 1;
1321
1322 DECL_STRUCT_FUNCTION(decl)->static_chain_decl = closure_decl;
1323 }
1324
1325 pop_cfun();
1326 }
1327 }
1328 }
1329 return this->fndecl_;
1330 }
1331
1332 // Get a tree for a function declaration.
1333
1334 tree
1335 Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no, tree id)
1336 {
1337 if (this->fndecl_ == NULL_TREE)
1338 {
1339 // Let Go code use an asm declaration to pick up a builtin
1340 // function.
1341 if (!this->asm_name_.empty())
1342 {
1343 std::map<std::string, tree>::const_iterator p =
1344 builtin_functions.find(this->asm_name_);
1345 if (p != builtin_functions.end())
1346 {
1347 this->fndecl_ = p->second;
1348 return this->fndecl_;
1349 }
1350 }
1351
1352 tree functype = this->fntype_->get_tree(gogo);
1353 tree decl;
1354 if (functype == error_mark_node)
1355 decl = error_mark_node;
1356 else
1357 {
1358 // The type of a function comes back as a pointer, but we
1359 // want the real function type for a function declaration.
1360 gcc_assert(POINTER_TYPE_P(functype));
1361 functype = TREE_TYPE(functype);
1362 decl = build_decl(this->location(), FUNCTION_DECL, id, functype);
1363 TREE_PUBLIC(decl) = 1;
1364 DECL_EXTERNAL(decl) = 1;
1365
1366 if (this->asm_name_.empty())
1367 {
1368 std::string asm_name = (no->package() == NULL
1369 ? gogo->unique_prefix()
1370 : no->package()->unique_prefix());
1371 asm_name.append(1, '.');
1372 asm_name.append(IDENTIFIER_POINTER(id), IDENTIFIER_LENGTH(id));
1373 SET_DECL_ASSEMBLER_NAME(decl,
1374 get_identifier_from_string(asm_name));
1375 }
1376 }
1377 this->fndecl_ = decl;
1378 go_preserve_from_gc(decl);
1379 }
1380 return this->fndecl_;
1381 }
1382
1383 // We always pass the receiver to a method as a pointer. If the
1384 // receiver is actually declared as a non-pointer type, then we copy
1385 // the value into a local variable, so that it has the right type. In
1386 // this function we create the real PARM_DECL to use, and set
1387 // DEC_INITIAL of the var_decl to be the value passed in.
1388
1389 tree
1390 Function::make_receiver_parm_decl(Gogo* gogo, Named_object* no, tree var_decl)
1391 {
1392 if (var_decl == error_mark_node)
1393 return error_mark_node;
1394 // If the function takes the address of a receiver which is passed
1395 // by value, then we will have an INDIRECT_REF here. We need to get
1396 // the real variable.
1397 bool is_in_heap = no->var_value()->is_in_heap();
1398 tree val_type;
1399 if (TREE_CODE(var_decl) != INDIRECT_REF)
1400 {
1401 gcc_assert(!is_in_heap);
1402 val_type = TREE_TYPE(var_decl);
1403 }
1404 else
1405 {
1406 gcc_assert(is_in_heap);
1407 var_decl = TREE_OPERAND(var_decl, 0);
1408 if (var_decl == error_mark_node)
1409 return error_mark_node;
1410 gcc_assert(POINTER_TYPE_P(TREE_TYPE(var_decl)));
1411 val_type = TREE_TYPE(TREE_TYPE(var_decl));
1412 }
1413 gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
1414 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1415 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1416 name += ".pointer";
1417 tree id = get_identifier_from_string(name);
1418 tree parm_decl = build_decl(loc, PARM_DECL, id, build_pointer_type(val_type));
1419 DECL_CONTEXT(parm_decl) = current_function_decl;
1420 DECL_ARG_TYPE(parm_decl) = TREE_TYPE(parm_decl);
1421
1422 gcc_assert(DECL_INITIAL(var_decl) == NULL_TREE);
1423 // The receiver might be passed as a null pointer.
1424 tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node, parm_decl,
1425 fold_convert_loc(loc, TREE_TYPE(parm_decl),
1426 null_pointer_node));
1427 tree ind = build_fold_indirect_ref_loc(loc, parm_decl);
1428 TREE_THIS_NOTRAP(ind) = 1;
1429 tree zero_init = no->var_value()->type()->get_init_tree(gogo, false);
1430 tree init = fold_build3_loc(loc, COND_EXPR, TREE_TYPE(ind),
1431 check, ind, zero_init);
1432
1433 if (is_in_heap)
1434 {
1435 tree size = TYPE_SIZE_UNIT(val_type);
1436 tree space = gogo->allocate_memory(no->var_value()->type(), size,
1437 no->location());
1438 space = save_expr(space);
1439 space = fold_convert(build_pointer_type(val_type), space);
1440 tree spaceref = build_fold_indirect_ref_loc(no->location(), space);
1441 TREE_THIS_NOTRAP(spaceref) = 1;
1442 tree check = fold_build2_loc(loc, NE_EXPR, boolean_type_node,
1443 parm_decl,
1444 fold_convert_loc(loc, TREE_TYPE(parm_decl),
1445 null_pointer_node));
1446 tree parmref = build_fold_indirect_ref_loc(no->location(), parm_decl);
1447 TREE_THIS_NOTRAP(parmref) = 1;
1448 tree set = fold_build2_loc(loc, MODIFY_EXPR, void_type_node,
1449 spaceref, parmref);
1450 init = fold_build2_loc(loc, COMPOUND_EXPR, TREE_TYPE(space),
1451 build3(COND_EXPR, void_type_node,
1452 check, set, NULL_TREE),
1453 space);
1454 }
1455
1456 DECL_INITIAL(var_decl) = init;
1457
1458 return parm_decl;
1459 }
1460
1461 // If we take the address of a parameter, then we need to copy it into
1462 // the heap. We will access it as a local variable via an
1463 // indirection.
1464
1465 tree
1466 Function::copy_parm_to_heap(Gogo* gogo, Named_object* no, tree ref)
1467 {
1468 if (ref == error_mark_node)
1469 return error_mark_node;
1470
1471 gcc_assert(TREE_CODE(ref) == INDIRECT_REF);
1472
1473 tree var_decl = TREE_OPERAND(ref, 0);
1474 if (var_decl == error_mark_node)
1475 return error_mark_node;
1476 gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
1477 source_location loc = DECL_SOURCE_LOCATION(var_decl);
1478
1479 std::string name = IDENTIFIER_POINTER(DECL_NAME(var_decl));
1480 name += ".param";
1481 tree id = get_identifier_from_string(name);
1482
1483 tree type = TREE_TYPE(var_decl);
1484 gcc_assert(POINTER_TYPE_P(type));
1485 type = TREE_TYPE(type);
1486
1487 tree parm_decl = build_decl(loc, PARM_DECL, id, type);
1488 DECL_CONTEXT(parm_decl) = current_function_decl;
1489 DECL_ARG_TYPE(parm_decl) = type;
1490
1491 tree size = TYPE_SIZE_UNIT(type);
1492 tree space = gogo->allocate_memory(no->var_value()->type(), size, loc);
1493 space = save_expr(space);
1494 space = fold_convert(TREE_TYPE(var_decl), space);
1495 tree spaceref = build_fold_indirect_ref_loc(loc, space);
1496 TREE_THIS_NOTRAP(spaceref) = 1;
1497 tree init = build2(COMPOUND_EXPR, TREE_TYPE(space),
1498 build2(MODIFY_EXPR, void_type_node, spaceref, parm_decl),
1499 space);
1500 DECL_INITIAL(var_decl) = init;
1501
1502 return parm_decl;
1503 }
1504
1505 // Get a tree for function code.
1506
1507 void
1508 Function::build_tree(Gogo* gogo, Named_object* named_function)
1509 {
1510 tree fndecl = this->fndecl_;
1511 gcc_assert(fndecl != NULL_TREE);
1512
1513 tree params = NULL_TREE;
1514 tree* pp = &params;
1515
1516 tree declare_vars = NULL_TREE;
1517 for (Bindings::const_definitions_iterator p =
1518 this->block_->bindings()->begin_definitions();
1519 p != this->block_->bindings()->end_definitions();
1520 ++p)
1521 {
1522 if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
1523 {
1524 *pp = (*p)->get_tree(gogo, named_function);
1525
1526 // We always pass the receiver to a method as a pointer. If
1527 // the receiver is declared as a non-pointer type, then we
1528 // copy the value into a local variable.
1529 if ((*p)->var_value()->is_receiver()
1530 && (*p)->var_value()->type()->points_to() == NULL)
1531 {
1532 tree parm_decl = this->make_receiver_parm_decl(gogo, *p, *pp);
1533 tree var = *pp;
1534 if (TREE_CODE(var) == INDIRECT_REF)
1535 var = TREE_OPERAND(var, 0);
1536 if (var != error_mark_node)
1537 {
1538 gcc_assert(TREE_CODE(var) == VAR_DECL);
1539 DECL_CHAIN(var) = declare_vars;
1540 declare_vars = var;
1541 }
1542 *pp = parm_decl;
1543 }
1544 else if ((*p)->var_value()->is_in_heap())
1545 {
1546 // If we take the address of a parameter, then we need
1547 // to copy it into the heap.
1548 tree parm_decl = this->copy_parm_to_heap(gogo, *p, *pp);
1549 if (*pp != error_mark_node)
1550 {
1551 gcc_assert(TREE_CODE(*pp) == INDIRECT_REF);
1552 tree var_decl = TREE_OPERAND(*pp, 0);
1553 if (var_decl != error_mark_node)
1554 {
1555 gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
1556 DECL_CHAIN(var_decl) = declare_vars;
1557 declare_vars = var_decl;
1558 }
1559 }
1560 *pp = parm_decl;
1561 }
1562
1563 if (*pp != error_mark_node)
1564 {
1565 gcc_assert(TREE_CODE(*pp) == PARM_DECL);
1566 pp = &DECL_CHAIN(*pp);
1567 }
1568 }
1569 else if ((*p)->is_result_variable())
1570 {
1571 tree var_decl = (*p)->get_tree(gogo, named_function);
1572 if (var_decl != error_mark_node
1573 && (*p)->result_var_value()->is_in_heap())
1574 {
1575 gcc_assert(TREE_CODE(var_decl) == INDIRECT_REF);
1576 var_decl = TREE_OPERAND(var_decl, 0);
1577 }
1578 if (var_decl != error_mark_node)
1579 {
1580 gcc_assert(TREE_CODE(var_decl) == VAR_DECL);
1581 DECL_CHAIN(var_decl) = declare_vars;
1582 declare_vars = var_decl;
1583 }
1584 }
1585 }
1586 *pp = NULL_TREE;
1587
1588 DECL_ARGUMENTS(fndecl) = params;
1589
1590 if (this->block_ != NULL)
1591 {
1592 gcc_assert(DECL_INITIAL(fndecl) == NULL_TREE);
1593
1594 // Declare variables if necessary.
1595 tree bind = NULL_TREE;
1596 if (declare_vars != NULL_TREE)
1597 {
1598 tree block = make_node(BLOCK);
1599 BLOCK_SUPERCONTEXT(block) = fndecl;
1600 DECL_INITIAL(fndecl) = block;
1601 BLOCK_VARS(block) = declare_vars;
1602 TREE_USED(block) = 1;
1603 bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block),
1604 NULL_TREE, block);
1605 TREE_SIDE_EFFECTS(bind) = 1;
1606 }
1607
1608 // Build the trees for all the statements in the function.
1609 Translate_context context(gogo, named_function, NULL, NULL_TREE);
1610 tree code = this->block_->get_tree(&context);
1611
1612 tree init = NULL_TREE;
1613 tree except = NULL_TREE;
1614 tree fini = NULL_TREE;
1615
1616 // Initialize variables if necessary.
1617 for (tree v = declare_vars; v != NULL_TREE; v = DECL_CHAIN(v))
1618 {
1619 tree dv = build1(DECL_EXPR, void_type_node, v);
1620 SET_EXPR_LOCATION(dv, DECL_SOURCE_LOCATION(v));
1621 append_to_statement_list(dv, &init);
1622 }
1623
1624 // If we have a defer stack, initialize it at the start of a
1625 // function.
1626 if (this->defer_stack_ != NULL_TREE)
1627 {
1628 tree defer_init = build1(DECL_EXPR, void_type_node,
1629 this->defer_stack_);
1630 SET_EXPR_LOCATION(defer_init, this->block_->start_location());
1631 append_to_statement_list(defer_init, &init);
1632
1633 // Clean up the defer stack when we leave the function.
1634 this->build_defer_wrapper(gogo, named_function, &except, &fini);
1635 }
1636
1637 if (code != NULL_TREE && code != error_mark_node)
1638 {
1639 if (init != NULL_TREE)
1640 code = build2(COMPOUND_EXPR, void_type_node, init, code);
1641 if (except != NULL_TREE)
1642 code = build2(TRY_CATCH_EXPR, void_type_node, code,
1643 build2(CATCH_EXPR, void_type_node, NULL, except));
1644 if (fini != NULL_TREE)
1645 code = build2(TRY_FINALLY_EXPR, void_type_node, code, fini);
1646 }
1647
1648 // Stick the code into the block we built for the receiver, if
1649 // we built on.
1650 if (bind != NULL_TREE && code != NULL_TREE && code != error_mark_node)
1651 {
1652 BIND_EXPR_BODY(bind) = code;
1653 code = bind;
1654 }
1655
1656 DECL_SAVED_TREE(fndecl) = code;
1657 }
1658 }
1659
1660 // Build the wrappers around function code needed if the function has
1661 // any defer statements. This sets *EXCEPT to an exception handler
1662 // and *FINI to a finally handler.
1663
1664 void
1665 Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
1666 tree *except, tree *fini)
1667 {
1668 source_location end_loc = this->block_->end_location();
1669
1670 // Add an exception handler. This is used if a panic occurs. Its
1671 // purpose is to stop the stack unwinding if a deferred function
1672 // calls recover. There are more details in
1673 // libgo/runtime/go-unwind.c.
1674 tree stmt_list = NULL_TREE;
1675 static tree check_fndecl;
1676 tree call = Gogo::call_builtin(&check_fndecl,
1677 end_loc,
1678 "__go_check_defer",
1679 1,
1680 void_type_node,
1681 ptr_type_node,
1682 this->defer_stack(end_loc));
1683 if (call != error_mark_node)
1684 append_to_statement_list(call, &stmt_list);
1685
1686 tree retval = this->return_value(gogo, named_function, end_loc, &stmt_list);
1687 tree set;
1688 if (retval == NULL_TREE)
1689 set = NULL_TREE;
1690 else
1691 set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1692 DECL_RESULT(this->fndecl_), retval);
1693 tree ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1694 append_to_statement_list(ret_stmt, &stmt_list);
1695
1696 gcc_assert(*except == NULL_TREE);
1697 *except = stmt_list;
1698
1699 // Add some finally code to run the defer functions. This is used
1700 // both in the normal case, when no panic occurs, and also if a
1701 // panic occurs to run any further defer functions. Of course, it
1702 // is possible for a defer function to call panic which should be
1703 // caught by another defer function. To handle that we use a loop.
1704 // finish:
1705 // try { __go_undefer(); } catch { __go_check_defer(); goto finish; }
1706 // if (return values are named) return named_vals;
1707
1708 stmt_list = NULL;
1709
1710 tree label = create_artificial_label(end_loc);
1711 tree define_label = fold_build1_loc(end_loc, LABEL_EXPR, void_type_node,
1712 label);
1713 append_to_statement_list(define_label, &stmt_list);
1714
1715 static tree undefer_fndecl;
1716 tree undefer = Gogo::call_builtin(&undefer_fndecl,
1717 end_loc,
1718 "__go_undefer",
1719 1,
1720 void_type_node,
1721 ptr_type_node,
1722 this->defer_stack(end_loc));
1723 if (undefer_fndecl != NULL_TREE)
1724 TREE_NOTHROW(undefer_fndecl) = 0;
1725
1726 tree defer = Gogo::call_builtin(&check_fndecl,
1727 end_loc,
1728 "__go_check_defer",
1729 1,
1730 void_type_node,
1731 ptr_type_node,
1732 this->defer_stack(end_loc));
1733 tree jump = fold_build1_loc(end_loc, GOTO_EXPR, void_type_node, label);
1734 tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer, jump);
1735 catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
1736 tree try_catch = build2(TRY_CATCH_EXPR, void_type_node, undefer, catch_body);
1737
1738 append_to_statement_list(try_catch, &stmt_list);
1739
1740 if (this->type_->results() != NULL
1741 && !this->type_->results()->empty()
1742 && !this->type_->results()->front().name().empty())
1743 {
1744 // If the result variables are named, we need to return them
1745 // again, because they might have been changed by a defer
1746 // function.
1747 retval = this->return_value(gogo, named_function, end_loc,
1748 &stmt_list);
1749 set = fold_build2_loc(end_loc, MODIFY_EXPR, void_type_node,
1750 DECL_RESULT(this->fndecl_), retval);
1751 ret_stmt = fold_build1_loc(end_loc, RETURN_EXPR, void_type_node, set);
1752 append_to_statement_list(ret_stmt, &stmt_list);
1753 }
1754
1755 gcc_assert(*fini == NULL_TREE);
1756 *fini = stmt_list;
1757 }
1758
1759 // Return the value to assign to DECL_RESULT(this->fndecl_). This may
1760 // also add statements to STMT_LIST, which need to be executed before
1761 // the assignment. This is used for a return statement with no
1762 // explicit values.
1763
1764 tree
1765 Function::return_value(Gogo* gogo, Named_object* named_function,
1766 source_location location, tree* stmt_list) const
1767 {
1768 const Typed_identifier_list* results = this->type_->results();
1769 if (results == NULL || results->empty())
1770 return NULL_TREE;
1771
1772 // In the case of an exception handler created for functions with
1773 // defer statements, the result variables may be unnamed.
1774 bool is_named = !results->front().name().empty();
1775 if (is_named)
1776 {
1777 gcc_assert(this->named_results_ != NULL);
1778 if (this->named_results_->size() != results->size())
1779 {
1780 gcc_assert(saw_errors());
1781 return error_mark_node;
1782 }
1783 }
1784
1785 tree retval;
1786 if (results->size() == 1)
1787 {
1788 if (is_named)
1789 return this->named_results_->front()->get_tree(gogo, named_function);
1790 else
1791 return results->front().type()->get_init_tree(gogo, false);
1792 }
1793 else
1794 {
1795 tree rettype = TREE_TYPE(DECL_RESULT(this->fndecl_));
1796 retval = create_tmp_var(rettype, "RESULT");
1797 tree field = TYPE_FIELDS(rettype);
1798 int index = 0;
1799 for (Typed_identifier_list::const_iterator pr = results->begin();
1800 pr != results->end();
1801 ++pr, ++index, field = DECL_CHAIN(field))
1802 {
1803 gcc_assert(field != NULL);
1804 tree val;
1805 if (is_named)
1806 val = (*this->named_results_)[index]->get_tree(gogo,
1807 named_function);
1808 else
1809 val = pr->type()->get_init_tree(gogo, false);
1810 tree set = fold_build2_loc(location, MODIFY_EXPR, void_type_node,
1811 build3(COMPONENT_REF, TREE_TYPE(field),
1812 retval, field, NULL_TREE),
1813 val);
1814 append_to_statement_list(set, stmt_list);
1815 }
1816 return retval;
1817 }
1818 }
1819
1820 // Get the tree for the variable holding the defer stack for this
1821 // function. At least at present, the value of this variable is not
1822 // used. However, a pointer to this variable is used as a marker for
1823 // the functions on the defer stack associated with this function.
1824 // Doing things this way permits inlining a function which uses defer.
1825
1826 tree
1827 Function::defer_stack(source_location location)
1828 {
1829 if (this->defer_stack_ == NULL_TREE)
1830 {
1831 tree var = create_tmp_var(ptr_type_node, "DEFER");
1832 DECL_INITIAL(var) = null_pointer_node;
1833 DECL_SOURCE_LOCATION(var) = location;
1834 TREE_ADDRESSABLE(var) = 1;
1835 this->defer_stack_ = var;
1836 }
1837 return fold_convert_loc(location, ptr_type_node,
1838 build_fold_addr_expr_loc(location,
1839 this->defer_stack_));
1840 }
1841
1842 // Get a tree for the statements in a block.
1843
1844 tree
1845 Block::get_tree(Translate_context* context)
1846 {
1847 Gogo* gogo = context->gogo();
1848
1849 tree block = make_node(BLOCK);
1850
1851 // Put the new block into the block tree.
1852
1853 if (context->block() == NULL)
1854 {
1855 tree fndecl;
1856 if (context->function() != NULL)
1857 fndecl = context->function()->func_value()->get_decl();
1858 else
1859 fndecl = current_function_decl;
1860 gcc_assert(fndecl != NULL_TREE);
1861
1862 // We may have already created a block for the receiver.
1863 if (DECL_INITIAL(fndecl) == NULL_TREE)
1864 {
1865 BLOCK_SUPERCONTEXT(block) = fndecl;
1866 DECL_INITIAL(fndecl) = block;
1867 }
1868 else
1869 {
1870 tree superblock_tree = DECL_INITIAL(fndecl);
1871 BLOCK_SUPERCONTEXT(block) = superblock_tree;
1872 gcc_assert(BLOCK_CHAIN(block) == NULL_TREE);
1873 BLOCK_CHAIN(block) = block;
1874 }
1875 }
1876 else
1877 {
1878 tree superblock_tree = context->block_tree();
1879 BLOCK_SUPERCONTEXT(block) = superblock_tree;
1880 tree* pp;
1881 for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
1882 *pp != NULL_TREE;
1883 pp = &BLOCK_CHAIN(*pp))
1884 ;
1885 *pp = block;
1886 }
1887
1888 // Expand local variables in the block.
1889
1890 tree* pp = &BLOCK_VARS(block);
1891 for (Bindings::const_definitions_iterator pv =
1892 this->bindings_->begin_definitions();
1893 pv != this->bindings_->end_definitions();
1894 ++pv)
1895 {
1896 if ((!(*pv)->is_variable() || !(*pv)->var_value()->is_parameter())
1897 && !(*pv)->is_result_variable()
1898 && !(*pv)->is_const())
1899 {
1900 tree var = (*pv)->get_tree(gogo, context->function());
1901 if (var != error_mark_node && TREE_TYPE(var) != error_mark_node)
1902 {
1903 if ((*pv)->is_variable() && (*pv)->var_value()->is_in_heap())
1904 {
1905 gcc_assert(TREE_CODE(var) == INDIRECT_REF);
1906 var = TREE_OPERAND(var, 0);
1907 gcc_assert(TREE_CODE(var) == VAR_DECL);
1908 }
1909 *pp = var;
1910 pp = &DECL_CHAIN(*pp);
1911 }
1912 }
1913 }
1914 *pp = NULL_TREE;
1915
1916 Translate_context subcontext(context->gogo(), context->function(),
1917 this, block);
1918
1919 tree statements = NULL_TREE;
1920
1921 // Expand the statements.
1922
1923 for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
1924 p != this->statements_.end();
1925 ++p)
1926 {
1927 tree statement = (*p)->get_tree(&subcontext);
1928 if (statement != error_mark_node)
1929 append_to_statement_list(statement, &statements);
1930 }
1931
1932 TREE_USED(block) = 1;
1933
1934 tree bind = build3(BIND_EXPR, void_type_node, BLOCK_VARS(block), statements,
1935 block);
1936 TREE_SIDE_EFFECTS(bind) = 1;
1937
1938 return bind;
1939 }
1940
1941 // Get the LABEL_DECL for a label.
1942
1943 tree
1944 Label::get_decl()
1945 {
1946 if (this->decl_ == NULL)
1947 {
1948 tree id = get_identifier_from_string(this->name_);
1949 this->decl_ = build_decl(this->location_, LABEL_DECL, id, void_type_node);
1950 DECL_CONTEXT(this->decl_) = current_function_decl;
1951 }
1952 return this->decl_;
1953 }
1954
1955 // Return an expression for the address of this label.
1956
1957 tree
1958 Label::get_addr(source_location location)
1959 {
1960 tree decl = this->get_decl();
1961 TREE_USED(decl) = 1;
1962 TREE_ADDRESSABLE(decl) = 1;
1963 return fold_convert_loc(location, ptr_type_node,
1964 build_fold_addr_expr_loc(location, decl));
1965 }
1966
1967 // Get the LABEL_DECL for an unnamed label.
1968
1969 tree
1970 Unnamed_label::get_decl()
1971 {
1972 if (this->decl_ == NULL)
1973 this->decl_ = create_artificial_label(this->location_);
1974 return this->decl_;
1975 }
1976
1977 // Get the LABEL_EXPR for an unnamed label.
1978
1979 tree
1980 Unnamed_label::get_definition()
1981 {
1982 tree t = build1(LABEL_EXPR, void_type_node, this->get_decl());
1983 SET_EXPR_LOCATION(t, this->location_);
1984 return t;
1985 }
1986
1987 // Return a goto to this label.
1988
1989 tree
1990 Unnamed_label::get_goto(source_location location)
1991 {
1992 tree t = build1(GOTO_EXPR, void_type_node, this->get_decl());
1993 SET_EXPR_LOCATION(t, location);
1994 return t;
1995 }
1996
1997 // Return the integer type to use for a size.
1998
1999 GO_EXTERN_C
2000 tree
2001 go_type_for_size(unsigned int bits, int unsignedp)
2002 {
2003 const char* name;
2004 switch (bits)
2005 {
2006 case 8:
2007 name = unsignedp ? "uint8" : "int8";
2008 break;
2009 case 16:
2010 name = unsignedp ? "uint16" : "int16";
2011 break;
2012 case 32:
2013 name = unsignedp ? "uint32" : "int32";
2014 break;
2015 case 64:
2016 name = unsignedp ? "uint64" : "int64";
2017 break;
2018 default:
2019 if (bits == POINTER_SIZE && unsignedp)
2020 name = "uintptr";
2021 else
2022 return NULL_TREE;
2023 }
2024 Type* type = Type::lookup_integer_type(name);
2025 return type->get_tree(go_get_gogo());
2026 }
2027
2028 // Return the type to use for a mode.
2029
2030 GO_EXTERN_C
2031 tree
2032 go_type_for_mode(enum machine_mode mode, int unsignedp)
2033 {
2034 // FIXME: This static_cast should be in machmode.h.
2035 enum mode_class mc = static_cast<enum mode_class>(GET_MODE_CLASS(mode));
2036 if (mc == MODE_INT)
2037 return go_type_for_size(GET_MODE_BITSIZE(mode), unsignedp);
2038 else if (mc == MODE_FLOAT)
2039 {
2040 Type* type;
2041 switch (GET_MODE_BITSIZE (mode))
2042 {
2043 case 32:
2044 type = Type::lookup_float_type("float32");
2045 break;
2046 case 64:
2047 type = Type::lookup_float_type("float64");
2048 break;
2049 default:
2050 // We have to check for long double in order to support
2051 // i386 excess precision.
2052 if (mode == TYPE_MODE(long_double_type_node))
2053 return long_double_type_node;
2054 return NULL_TREE;
2055 }
2056 return type->float_type()->type_tree();
2057 }
2058 else if (mc == MODE_COMPLEX_FLOAT)
2059 {
2060 Type *type;
2061 switch (GET_MODE_BITSIZE (mode))
2062 {
2063 case 64:
2064 type = Type::lookup_complex_type("complex64");
2065 break;
2066 case 128:
2067 type = Type::lookup_complex_type("complex128");
2068 break;
2069 default:
2070 // We have to check for long double in order to support
2071 // i386 excess precision.
2072 if (mode == TYPE_MODE(complex_long_double_type_node))
2073 return complex_long_double_type_node;
2074 return NULL_TREE;
2075 }
2076 return type->complex_type()->type_tree();
2077 }
2078 else
2079 return NULL_TREE;
2080 }
2081
2082 // Return a tree which allocates SIZE bytes which will holds value of
2083 // type TYPE.
2084
2085 tree
2086 Gogo::allocate_memory(Type* type, tree size, source_location location)
2087 {
2088 // If the package imports unsafe, then it may play games with
2089 // pointers that look like integers.
2090 if (this->imported_unsafe_ || type->has_pointer())
2091 {
2092 static tree new_fndecl;
2093 return Gogo::call_builtin(&new_fndecl,
2094 location,
2095 "__go_new",
2096 1,
2097 ptr_type_node,
2098 sizetype,
2099 size);
2100 }
2101 else
2102 {
2103 static tree new_nopointers_fndecl;
2104 return Gogo::call_builtin(&new_nopointers_fndecl,
2105 location,
2106 "__go_new_nopointers",
2107 1,
2108 ptr_type_node,
2109 sizetype,
2110 size);
2111 }
2112 }
2113
2114 // Build a builtin struct with a list of fields. The name is
2115 // STRUCT_NAME. STRUCT_TYPE is NULL_TREE or an empty RECORD_TYPE
2116 // node; this exists so that the struct can have fields which point to
2117 // itself. If PTYPE is not NULL, store the result in *PTYPE. There
2118 // are NFIELDS fields. Each field is a name (a const char*) followed
2119 // by a type (a tree).
2120
2121 tree
2122 Gogo::builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
2123 int nfields, ...)
2124 {
2125 if (ptype != NULL && *ptype != NULL_TREE)
2126 return *ptype;
2127
2128 va_list ap;
2129 va_start(ap, nfields);
2130
2131 tree fields = NULL_TREE;
2132 for (int i = 0; i < nfields; ++i)
2133 {
2134 const char* field_name = va_arg(ap, const char*);
2135 tree type = va_arg(ap, tree);
2136 if (type == error_mark_node)
2137 {
2138 if (ptype != NULL)
2139 *ptype = error_mark_node;
2140 return error_mark_node;
2141 }
2142 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL,
2143 get_identifier(field_name), type);
2144 DECL_CHAIN(field) = fields;
2145 fields = field;
2146 }
2147
2148 va_end(ap);
2149
2150 if (struct_type == NULL_TREE)
2151 struct_type = make_node(RECORD_TYPE);
2152 finish_builtin_struct(struct_type, struct_name, fields, NULL_TREE);
2153
2154 if (ptype != NULL)
2155 {
2156 go_preserve_from_gc(struct_type);
2157 *ptype = struct_type;
2158 }
2159
2160 return struct_type;
2161 }
2162
2163 // Return a type to use for pointer to const char for a string.
2164
2165 tree
2166 Gogo::const_char_pointer_type_tree()
2167 {
2168 static tree type;
2169 if (type == NULL_TREE)
2170 {
2171 tree const_char_type = build_qualified_type(unsigned_char_type_node,
2172 TYPE_QUAL_CONST);
2173 type = build_pointer_type(const_char_type);
2174 go_preserve_from_gc(type);
2175 }
2176 return type;
2177 }
2178
2179 // Return a tree for a string constant.
2180
2181 tree
2182 Gogo::string_constant_tree(const std::string& val)
2183 {
2184 tree index_type = build_index_type(size_int(val.length()));
2185 tree const_char_type = build_qualified_type(unsigned_char_type_node,
2186 TYPE_QUAL_CONST);
2187 tree string_type = build_array_type(const_char_type, index_type);
2188 string_type = build_variant_type_copy(string_type);
2189 TYPE_STRING_FLAG(string_type) = 1;
2190 tree string_val = build_string(val.length(), val.data());
2191 TREE_TYPE(string_val) = string_type;
2192 return string_val;
2193 }
2194
2195 // Return a tree for a Go string constant.
2196
2197 tree
2198 Gogo::go_string_constant_tree(const std::string& val)
2199 {
2200 tree string_type = Type::make_string_type()->get_tree(this);
2201
2202 VEC(constructor_elt, gc)* init = VEC_alloc(constructor_elt, gc, 2);
2203
2204 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2205 tree field = TYPE_FIELDS(string_type);
2206 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__data") == 0);
2207 elt->index = field;
2208 tree str = Gogo::string_constant_tree(val);
2209 elt->value = fold_convert(TREE_TYPE(field),
2210 build_fold_addr_expr(str));
2211
2212 elt = VEC_quick_push(constructor_elt, init, NULL);
2213 field = DECL_CHAIN(field);
2214 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__length") == 0);
2215 elt->index = field;
2216 elt->value = build_int_cst_type(TREE_TYPE(field), val.length());
2217
2218 tree constructor = build_constructor(string_type, init);
2219 TREE_READONLY(constructor) = 1;
2220 TREE_CONSTANT(constructor) = 1;
2221
2222 return constructor;
2223 }
2224
2225 // Return a tree for a pointer to a Go string constant. This is only
2226 // used for type descriptors, so we return a pointer to a constant
2227 // decl.
2228
2229 tree
2230 Gogo::ptr_go_string_constant_tree(const std::string& val)
2231 {
2232 tree pval = this->go_string_constant_tree(val);
2233
2234 tree decl = build_decl(UNKNOWN_LOCATION, VAR_DECL,
2235 create_tmp_var_name("SP"), TREE_TYPE(pval));
2236 DECL_EXTERNAL(decl) = 0;
2237 TREE_PUBLIC(decl) = 0;
2238 TREE_USED(decl) = 1;
2239 TREE_READONLY(decl) = 1;
2240 TREE_CONSTANT(decl) = 1;
2241 TREE_STATIC(decl) = 1;
2242 DECL_ARTIFICIAL(decl) = 1;
2243 DECL_INITIAL(decl) = pval;
2244 rest_of_decl_compilation(decl, 1, 0);
2245
2246 return build_fold_addr_expr(decl);
2247 }
2248
2249 // Build the type of the struct that holds a slice for the given
2250 // element type.
2251
2252 tree
2253 Gogo::slice_type_tree(tree element_type_tree)
2254 {
2255 // We use int for the count and capacity fields in a slice header.
2256 // This matches 6g. The language definition guarantees that we
2257 // can't allocate space of a size which does not fit in int
2258 // anyhow. FIXME: integer_type_node is the the C type "int" but is
2259 // not necessarily the Go type "int". They will differ when the C
2260 // type "int" has fewer than 32 bits.
2261 return Gogo::builtin_struct(NULL, "__go_slice", NULL_TREE, 3,
2262 "__values",
2263 build_pointer_type(element_type_tree),
2264 "__count",
2265 integer_type_node,
2266 "__capacity",
2267 integer_type_node);
2268 }
2269
2270 // Given the tree for a slice type, return the tree for the type of
2271 // the elements of the slice.
2272
2273 tree
2274 Gogo::slice_element_type_tree(tree slice_type_tree)
2275 {
2276 gcc_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE
2277 && POINTER_TYPE_P(TREE_TYPE(TYPE_FIELDS(slice_type_tree))));
2278 return TREE_TYPE(TREE_TYPE(TYPE_FIELDS(slice_type_tree)));
2279 }
2280
2281 // Build a constructor for a slice. SLICE_TYPE_TREE is the type of
2282 // the slice. VALUES is the value pointer and COUNT is the number of
2283 // entries. If CAPACITY is not NULL, it is the capacity; otherwise
2284 // the capacity and the count are the same.
2285
2286 tree
2287 Gogo::slice_constructor(tree slice_type_tree, tree values, tree count,
2288 tree capacity)
2289 {
2290 gcc_assert(TREE_CODE(slice_type_tree) == RECORD_TYPE);
2291
2292 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
2293
2294 tree field = TYPE_FIELDS(slice_type_tree);
2295 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
2296 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
2297 elt->index = field;
2298 gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(field))
2299 == TYPE_MAIN_VARIANT(TREE_TYPE(values)));
2300 elt->value = values;
2301
2302 count = fold_convert(sizetype, count);
2303 if (capacity == NULL_TREE)
2304 {
2305 count = save_expr(count);
2306 capacity = count;
2307 }
2308
2309 field = DECL_CHAIN(field);
2310 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
2311 elt = VEC_quick_push(constructor_elt, init, NULL);
2312 elt->index = field;
2313 elt->value = fold_convert(TREE_TYPE(field), count);
2314
2315 field = DECL_CHAIN(field);
2316 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
2317 elt = VEC_quick_push(constructor_elt, init, NULL);
2318 elt->index = field;
2319 elt->value = fold_convert(TREE_TYPE(field), capacity);
2320
2321 return build_constructor(slice_type_tree, init);
2322 }
2323
2324 // Build a constructor for an empty slice.
2325
2326 tree
2327 Gogo::empty_slice_constructor(tree slice_type_tree)
2328 {
2329 tree element_field = TYPE_FIELDS(slice_type_tree);
2330 tree ret = Gogo::slice_constructor(slice_type_tree,
2331 fold_convert(TREE_TYPE(element_field),
2332 null_pointer_node),
2333 size_zero_node,
2334 size_zero_node);
2335 TREE_CONSTANT(ret) = 1;
2336 return ret;
2337 }
2338
2339 // Build a map descriptor for a map of type MAPTYPE.
2340
2341 tree
2342 Gogo::map_descriptor(Map_type* maptype)
2343 {
2344 if (this->map_descriptors_ == NULL)
2345 this->map_descriptors_ = new Map_descriptors(10);
2346
2347 std::pair<const Map_type*, tree> val(maptype, NULL);
2348 std::pair<Map_descriptors::iterator, bool> ins =
2349 this->map_descriptors_->insert(val);
2350 Map_descriptors::iterator p = ins.first;
2351 if (!ins.second)
2352 {
2353 if (p->second == error_mark_node)
2354 return error_mark_node;
2355 gcc_assert(p->second != NULL_TREE && DECL_P(p->second));
2356 return build_fold_addr_expr(p->second);
2357 }
2358
2359 Type* keytype = maptype->key_type();
2360 Type* valtype = maptype->val_type();
2361
2362 std::string mangled_name = ("__go_map_" + maptype->mangled_name(this));
2363
2364 tree id = get_identifier_from_string(mangled_name);
2365
2366 // Get the type of the map descriptor. This is __go_map_descriptor
2367 // in libgo/map.h.
2368
2369 tree struct_type = this->map_descriptor_type();
2370
2371 // The map entry type is a struct with three fields. This struct is
2372 // specific to MAPTYPE. Build it.
2373
2374 tree map_entry_type = make_node(RECORD_TYPE);
2375
2376 map_entry_type = Gogo::builtin_struct(NULL, "__map", map_entry_type, 3,
2377 "__next",
2378 build_pointer_type(map_entry_type),
2379 "__key",
2380 keytype->get_tree(this),
2381 "__val",
2382 valtype->get_tree(this));
2383 if (map_entry_type == error_mark_node)
2384 {
2385 p->second = error_mark_node;
2386 return error_mark_node;
2387 }
2388
2389 tree map_entry_key_field = DECL_CHAIN(TYPE_FIELDS(map_entry_type));
2390 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_key_field)),
2391 "__key") == 0);
2392
2393 tree map_entry_val_field = DECL_CHAIN(map_entry_key_field);
2394 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_entry_val_field)),
2395 "__val") == 0);
2396
2397 // Initialize the entries.
2398
2399 tree map_descriptor_field = TYPE_FIELDS(struct_type);
2400 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(map_descriptor_field)),
2401 "__map_descriptor") == 0);
2402 tree entry_size_field = DECL_CHAIN(map_descriptor_field);
2403 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(entry_size_field)),
2404 "__entry_size") == 0);
2405 tree key_offset_field = DECL_CHAIN(entry_size_field);
2406 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(key_offset_field)),
2407 "__key_offset") == 0);
2408 tree val_offset_field = DECL_CHAIN(key_offset_field);
2409 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(val_offset_field)),
2410 "__val_offset") == 0);
2411
2412 VEC(constructor_elt, gc)* descriptor = VEC_alloc(constructor_elt, gc, 6);
2413
2414 constructor_elt* elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2415 elt->index = map_descriptor_field;
2416 elt->value = maptype->type_descriptor_pointer(this);
2417
2418 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2419 elt->index = entry_size_field;
2420 elt->value = TYPE_SIZE_UNIT(map_entry_type);
2421
2422 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2423 elt->index = key_offset_field;
2424 elt->value = byte_position(map_entry_key_field);
2425
2426 elt = VEC_quick_push(constructor_elt, descriptor, NULL);
2427 elt->index = val_offset_field;
2428 elt->value = byte_position(map_entry_val_field);
2429
2430 tree constructor = build_constructor(struct_type, descriptor);
2431
2432 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, struct_type);
2433 TREE_STATIC(decl) = 1;
2434 TREE_USED(decl) = 1;
2435 TREE_READONLY(decl) = 1;
2436 TREE_CONSTANT(decl) = 1;
2437 DECL_INITIAL(decl) = constructor;
2438 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2439 resolve_unique_section(decl, 1, 0);
2440
2441 rest_of_decl_compilation(decl, 1, 0);
2442
2443 go_preserve_from_gc(decl);
2444 p->second = decl;
2445
2446 return build_fold_addr_expr(decl);
2447 }
2448
2449 // Return a tree for the type of a map descriptor. This is struct
2450 // __go_map_descriptor in libgo/runtime/map.h. This is the same for
2451 // all map types.
2452
2453 tree
2454 Gogo::map_descriptor_type()
2455 {
2456 static tree struct_type;
2457 tree dtype = Type::make_type_descriptor_type()->get_tree(this);
2458 dtype = build_qualified_type(dtype, TYPE_QUAL_CONST);
2459 return Gogo::builtin_struct(&struct_type, "__go_map_descriptor", NULL_TREE,
2460 4,
2461 "__map_descriptor",
2462 build_pointer_type(dtype),
2463 "__entry_size",
2464 sizetype,
2465 "__key_offset",
2466 sizetype,
2467 "__val_offset",
2468 sizetype);
2469 }
2470
2471 // Return the name to use for a type descriptor decl for TYPE. This
2472 // is used when TYPE does not have a name.
2473
2474 std::string
2475 Gogo::unnamed_type_descriptor_decl_name(const Type* type)
2476 {
2477 return "__go_td_" + type->mangled_name(this);
2478 }
2479
2480 // Return the name to use for a type descriptor decl for a type named
2481 // NAME, defined in the function IN_FUNCTION. IN_FUNCTION will
2482 // normally be NULL.
2483
2484 std::string
2485 Gogo::type_descriptor_decl_name(const Named_object* no,
2486 const Named_object* in_function)
2487 {
2488 std::string ret = "__go_tdn_";
2489 if (no->type_value()->is_builtin())
2490 gcc_assert(in_function == NULL);
2491 else
2492 {
2493 const std::string& unique_prefix(no->package() == NULL
2494 ? this->unique_prefix()
2495 : no->package()->unique_prefix());
2496 const std::string& package_name(no->package() == NULL
2497 ? this->package_name()
2498 : no->package()->name());
2499 ret.append(unique_prefix);
2500 ret.append(1, '.');
2501 ret.append(package_name);
2502 ret.append(1, '.');
2503 if (in_function != NULL)
2504 {
2505 ret.append(Gogo::unpack_hidden_name(in_function->name()));
2506 ret.append(1, '.');
2507 }
2508 }
2509 ret.append(no->name());
2510 return ret;
2511 }
2512
2513 // Where a type descriptor decl should be defined.
2514
2515 Gogo::Type_descriptor_location
2516 Gogo::type_descriptor_location(const Type* type)
2517 {
2518 const Named_type* name = type->named_type();
2519 if (name != NULL)
2520 {
2521 if (name->named_object()->package() != NULL)
2522 {
2523 // This is a named type defined in a different package. The
2524 // descriptor should be defined in that package.
2525 return TYPE_DESCRIPTOR_UNDEFINED;
2526 }
2527 else if (name->is_builtin())
2528 {
2529 // We create the descriptor for a builtin type whenever we
2530 // need it.
2531 return TYPE_DESCRIPTOR_COMMON;
2532 }
2533 else
2534 {
2535 // This is a named type defined in this package. The
2536 // descriptor should be defined here.
2537 return TYPE_DESCRIPTOR_DEFINED;
2538 }
2539 }
2540 else
2541 {
2542 if (type->points_to() != NULL
2543 && type->points_to()->named_type() != NULL
2544 && type->points_to()->named_type()->named_object()->package() != NULL)
2545 {
2546 // This is an unnamed pointer to a named type defined in a
2547 // different package. The descriptor should be defined in
2548 // that package.
2549 return TYPE_DESCRIPTOR_UNDEFINED;
2550 }
2551 else
2552 {
2553 // This is an unnamed type. The descriptor could be defined
2554 // in any package where it is needed, and the linker will
2555 // pick one descriptor to keep.
2556 return TYPE_DESCRIPTOR_COMMON;
2557 }
2558 }
2559 }
2560
2561 // Build a type descriptor decl for TYPE. INITIALIZER is a struct
2562 // composite literal which initializers the type descriptor.
2563
2564 void
2565 Gogo::build_type_descriptor_decl(const Type* type, Expression* initializer,
2566 tree* pdecl)
2567 {
2568 const Named_type* name = type->named_type();
2569
2570 // We can have multiple instances of unnamed types, but we only want
2571 // to emit the type descriptor once. We use a hash table to handle
2572 // this. This is not necessary for named types, as they are unique,
2573 // and we store the type descriptor decl in the type itself.
2574 tree* phash = NULL;
2575 if (name == NULL)
2576 {
2577 if (this->type_descriptor_decls_ == NULL)
2578 this->type_descriptor_decls_ = new Type_descriptor_decls(10);
2579
2580 std::pair<Type_descriptor_decls::iterator, bool> ins =
2581 this->type_descriptor_decls_->insert(std::make_pair(type, NULL_TREE));
2582 if (!ins.second)
2583 {
2584 // We've already built a type descriptor for this type.
2585 *pdecl = ins.first->second;
2586 return;
2587 }
2588 phash = &ins.first->second;
2589 }
2590
2591 std::string decl_name;
2592 if (name == NULL)
2593 decl_name = this->unnamed_type_descriptor_decl_name(type);
2594 else
2595 decl_name = this->type_descriptor_decl_name(name->named_object(),
2596 name->in_function());
2597 tree id = get_identifier_from_string(decl_name);
2598 tree descriptor_type_tree = initializer->type()->get_tree(this);
2599 if (descriptor_type_tree == error_mark_node)
2600 {
2601 *pdecl = error_mark_node;
2602 return;
2603 }
2604 tree decl = build_decl(name == NULL ? BUILTINS_LOCATION : name->location(),
2605 VAR_DECL, id,
2606 build_qualified_type(descriptor_type_tree,
2607 TYPE_QUAL_CONST));
2608 TREE_READONLY(decl) = 1;
2609 TREE_CONSTANT(decl) = 1;
2610 DECL_ARTIFICIAL(decl) = 1;
2611
2612 go_preserve_from_gc(decl);
2613 if (phash != NULL)
2614 *phash = decl;
2615
2616 // We store the new DECL now because we may need to refer to it when
2617 // expanding INITIALIZER.
2618 *pdecl = decl;
2619
2620 // If appropriate, just refer to the exported type identifier.
2621 Gogo::Type_descriptor_location type_descriptor_location =
2622 this->type_descriptor_location(type);
2623 if (type_descriptor_location == TYPE_DESCRIPTOR_UNDEFINED)
2624 {
2625 TREE_PUBLIC(decl) = 1;
2626 DECL_EXTERNAL(decl) = 1;
2627 return;
2628 }
2629
2630 TREE_STATIC(decl) = 1;
2631 TREE_USED(decl) = 1;
2632
2633 Translate_context context(this, NULL, NULL, NULL);
2634 context.set_is_const();
2635 tree constructor = initializer->get_tree(&context);
2636
2637 if (constructor == error_mark_node)
2638 gcc_assert(saw_errors());
2639
2640 DECL_INITIAL(decl) = constructor;
2641
2642 if (type_descriptor_location == TYPE_DESCRIPTOR_DEFINED)
2643 TREE_PUBLIC(decl) = 1;
2644 else
2645 {
2646 gcc_assert(type_descriptor_location == TYPE_DESCRIPTOR_COMMON);
2647 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2648 resolve_unique_section(decl, 1, 0);
2649 }
2650
2651 rest_of_decl_compilation(decl, 1, 0);
2652 }
2653
2654 // Build an interface method table for a type: a list of function
2655 // pointers, one for each interface method. This is used for
2656 // interfaces.
2657
2658 tree
2659 Gogo::interface_method_table_for_type(const Interface_type* interface,
2660 Named_type* type,
2661 bool is_pointer)
2662 {
2663 const Typed_identifier_list* interface_methods = interface->methods();
2664 gcc_assert(!interface_methods->empty());
2665
2666 std::string mangled_name = ((is_pointer ? "__go_pimt__" : "__go_imt_")
2667 + interface->mangled_name(this)
2668 + "__"
2669 + type->mangled_name(this));
2670
2671 tree id = get_identifier_from_string(mangled_name);
2672
2673 // See whether this interface has any hidden methods.
2674 bool has_hidden_methods = false;
2675 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2676 p != interface_methods->end();
2677 ++p)
2678 {
2679 if (Gogo::is_hidden_name(p->name()))
2680 {
2681 has_hidden_methods = true;
2682 break;
2683 }
2684 }
2685
2686 // We already know that the named type is convertible to the
2687 // interface. If the interface has hidden methods, and the named
2688 // type is defined in a different package, then the interface
2689 // conversion table will be defined by that other package.
2690 if (has_hidden_methods && type->named_object()->package() != NULL)
2691 {
2692 tree array_type = build_array_type(const_ptr_type_node, NULL);
2693 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2694 TREE_READONLY(decl) = 1;
2695 TREE_CONSTANT(decl) = 1;
2696 TREE_PUBLIC(decl) = 1;
2697 DECL_EXTERNAL(decl) = 1;
2698 go_preserve_from_gc(decl);
2699 return decl;
2700 }
2701
2702 size_t count = interface_methods->size();
2703 VEC(constructor_elt, gc)* pointers = VEC_alloc(constructor_elt, gc,
2704 count + 1);
2705
2706 // The first element is the type descriptor.
2707 constructor_elt* elt = VEC_quick_push(constructor_elt, pointers, NULL);
2708 elt->index = size_zero_node;
2709 Type* td_type;
2710 if (!is_pointer)
2711 td_type = type;
2712 else
2713 td_type = Type::make_pointer_type(type);
2714 elt->value = fold_convert(const_ptr_type_node,
2715 td_type->type_descriptor_pointer(this));
2716
2717 size_t i = 1;
2718 for (Typed_identifier_list::const_iterator p = interface_methods->begin();
2719 p != interface_methods->end();
2720 ++p, ++i)
2721 {
2722 bool is_ambiguous;
2723 Method* m = type->method_function(p->name(), &is_ambiguous);
2724 gcc_assert(m != NULL);
2725
2726 Named_object* no = m->named_object();
2727
2728 tree fnid = no->get_id(this);
2729
2730 tree fndecl;
2731 if (no->is_function())
2732 fndecl = no->func_value()->get_or_make_decl(this, no, fnid);
2733 else if (no->is_function_declaration())
2734 fndecl = no->func_declaration_value()->get_or_make_decl(this, no,
2735 fnid);
2736 else
2737 gcc_unreachable();
2738 fndecl = build_fold_addr_expr(fndecl);
2739
2740 elt = VEC_quick_push(constructor_elt, pointers, NULL);
2741 elt->index = size_int(i);
2742 elt->value = fold_convert(const_ptr_type_node, fndecl);
2743 }
2744 gcc_assert(i == count + 1);
2745
2746 tree array_type = build_array_type(const_ptr_type_node,
2747 build_index_type(size_int(count)));
2748 tree constructor = build_constructor(array_type, pointers);
2749
2750 tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL, id, array_type);
2751 TREE_STATIC(decl) = 1;
2752 TREE_USED(decl) = 1;
2753 TREE_READONLY(decl) = 1;
2754 TREE_CONSTANT(decl) = 1;
2755 DECL_INITIAL(decl) = constructor;
2756
2757 // If the interface type has hidden methods, then this is the only
2758 // definition of the table. Otherwise it is a comdat table which
2759 // may be defined in multiple packages.
2760 if (has_hidden_methods)
2761 TREE_PUBLIC(decl) = 1;
2762 else
2763 {
2764 make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
2765 resolve_unique_section(decl, 1, 0);
2766 }
2767
2768 rest_of_decl_compilation(decl, 1, 0);
2769
2770 go_preserve_from_gc(decl);
2771
2772 return decl;
2773 }
2774
2775 // Mark a function as a builtin library function.
2776
2777 void
2778 Gogo::mark_fndecl_as_builtin_library(tree fndecl)
2779 {
2780 DECL_EXTERNAL(fndecl) = 1;
2781 TREE_PUBLIC(fndecl) = 1;
2782 DECL_ARTIFICIAL(fndecl) = 1;
2783 TREE_NOTHROW(fndecl) = 1;
2784 DECL_VISIBILITY(fndecl) = VISIBILITY_DEFAULT;
2785 DECL_VISIBILITY_SPECIFIED(fndecl) = 1;
2786 }
2787
2788 // Build a call to a builtin function.
2789
2790 tree
2791 Gogo::call_builtin(tree* pdecl, source_location location, const char* name,
2792 int nargs, tree rettype, ...)
2793 {
2794 if (rettype == error_mark_node)
2795 return error_mark_node;
2796
2797 tree* types = new tree[nargs];
2798 tree* args = new tree[nargs];
2799
2800 va_list ap;
2801 va_start(ap, rettype);
2802 for (int i = 0; i < nargs; ++i)
2803 {
2804 types[i] = va_arg(ap, tree);
2805 args[i] = va_arg(ap, tree);
2806 if (types[i] == error_mark_node || args[i] == error_mark_node)
2807 {
2808 delete[] types;
2809 delete[] args;
2810 return error_mark_node;
2811 }
2812 }
2813 va_end(ap);
2814
2815 if (*pdecl == NULL_TREE)
2816 {
2817 tree fnid = get_identifier(name);
2818
2819 tree argtypes = NULL_TREE;
2820 tree* pp = &argtypes;
2821 for (int i = 0; i < nargs; ++i)
2822 {
2823 *pp = tree_cons(NULL_TREE, types[i], NULL_TREE);
2824 pp = &TREE_CHAIN(*pp);
2825 }
2826 *pp = void_list_node;
2827
2828 tree fntype = build_function_type(rettype, argtypes);
2829
2830 *pdecl = build_decl(BUILTINS_LOCATION, FUNCTION_DECL, fnid, fntype);
2831 Gogo::mark_fndecl_as_builtin_library(*pdecl);
2832 go_preserve_from_gc(*pdecl);
2833 }
2834
2835 tree fnptr = build_fold_addr_expr(*pdecl);
2836 if (CAN_HAVE_LOCATION_P(fnptr))
2837 SET_EXPR_LOCATION(fnptr, location);
2838
2839 tree ret = build_call_array(rettype, fnptr, nargs, args);
2840 SET_EXPR_LOCATION(ret, location);
2841
2842 delete[] types;
2843 delete[] args;
2844
2845 return ret;
2846 }
2847
2848 // Build a call to the runtime error function.
2849
2850 tree
2851 Gogo::runtime_error(int code, source_location location)
2852 {
2853 static tree runtime_error_fndecl;
2854 tree ret = Gogo::call_builtin(&runtime_error_fndecl,
2855 location,
2856 "__go_runtime_error",
2857 1,
2858 void_type_node,
2859 integer_type_node,
2860 build_int_cst(integer_type_node, code));
2861 if (ret == error_mark_node)
2862 return error_mark_node;
2863 // The runtime error function panics and does not return.
2864 TREE_NOTHROW(runtime_error_fndecl) = 0;
2865 TREE_THIS_VOLATILE(runtime_error_fndecl) = 1;
2866 return ret;
2867 }
2868
2869 // Send VAL on CHANNEL. If BLOCKING is true, the resulting tree has a
2870 // void type. If BLOCKING is false, the resulting tree has a boolean
2871 // type, and it will evaluate as true if the value was sent. If
2872 // FOR_SELECT is true, this is being done because it was chosen in a
2873 // select statement.
2874
2875 tree
2876 Gogo::send_on_channel(tree channel, tree val, bool blocking, bool for_select,
2877 source_location location)
2878 {
2879 if (channel == error_mark_node || val == error_mark_node)
2880 return error_mark_node;
2881
2882 if (int_size_in_bytes(TREE_TYPE(val)) <= 8
2883 && !AGGREGATE_TYPE_P(TREE_TYPE(val))
2884 && !FLOAT_TYPE_P(TREE_TYPE(val)))
2885 {
2886 val = convert_to_integer(uint64_type_node, val);
2887 if (blocking)
2888 {
2889 static tree send_small_fndecl;
2890 tree ret = Gogo::call_builtin(&send_small_fndecl,
2891 location,
2892 "__go_send_small",
2893 3,
2894 void_type_node,
2895 ptr_type_node,
2896 channel,
2897 uint64_type_node,
2898 val,
2899 boolean_type_node,
2900 (for_select
2901 ? boolean_true_node
2902 : boolean_false_node));
2903 if (ret == error_mark_node)
2904 return error_mark_node;
2905 // This can panic if there are too many operations on a
2906 // closed channel.
2907 TREE_NOTHROW(send_small_fndecl) = 0;
2908 return ret;
2909 }
2910 else
2911 {
2912 gcc_assert(!for_select);
2913 static tree send_nonblocking_small_fndecl;
2914 tree ret = Gogo::call_builtin(&send_nonblocking_small_fndecl,
2915 location,
2916 "__go_send_nonblocking_small",
2917 2,
2918 boolean_type_node,
2919 ptr_type_node,
2920 channel,
2921 uint64_type_node,
2922 val);
2923 if (ret == error_mark_node)
2924 return error_mark_node;
2925 // This can panic if there are too many operations on a
2926 // closed channel.
2927 TREE_NOTHROW(send_nonblocking_small_fndecl) = 0;
2928 return ret;
2929 }
2930 }
2931 else
2932 {
2933 tree make_tmp;
2934 if (TREE_ADDRESSABLE(TREE_TYPE(val)) || TREE_CODE(val) == VAR_DECL)
2935 {
2936 make_tmp = NULL_TREE;
2937 val = build_fold_addr_expr(val);
2938 if (DECL_P(val))
2939 TREE_ADDRESSABLE(val) = 1;
2940 }
2941 else
2942 {
2943 tree tmp = create_tmp_var(TREE_TYPE(val), get_name(val));
2944 DECL_IGNORED_P(tmp) = 0;
2945 DECL_INITIAL(tmp) = val;
2946 TREE_ADDRESSABLE(tmp) = 1;
2947 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
2948 SET_EXPR_LOCATION(make_tmp, location);
2949 val = build_fold_addr_expr(tmp);
2950 }
2951 val = fold_convert(ptr_type_node, val);
2952
2953 tree call;
2954 if (blocking)
2955 {
2956 static tree send_big_fndecl;
2957 call = Gogo::call_builtin(&send_big_fndecl,
2958 location,
2959 "__go_send_big",
2960 3,
2961 void_type_node,
2962 ptr_type_node,
2963 channel,
2964 ptr_type_node,
2965 val,
2966 boolean_type_node,
2967 (for_select
2968 ? boolean_true_node
2969 : boolean_false_node));
2970 if (call == error_mark_node)
2971 return error_mark_node;
2972 // This can panic if there are too many operations on a
2973 // closed channel.
2974 TREE_NOTHROW(send_big_fndecl) = 0;
2975 }
2976 else
2977 {
2978 gcc_assert(!for_select);
2979 static tree send_nonblocking_big_fndecl;
2980 call = Gogo::call_builtin(&send_nonblocking_big_fndecl,
2981 location,
2982 "__go_send_nonblocking_big",
2983 2,
2984 boolean_type_node,
2985 ptr_type_node,
2986 channel,
2987 ptr_type_node,
2988 val);
2989 if (call == error_mark_node)
2990 return error_mark_node;
2991 // This can panic if there are too many operations on a
2992 // closed channel.
2993 TREE_NOTHROW(send_nonblocking_big_fndecl) = 0;
2994 }
2995
2996 if (make_tmp == NULL_TREE)
2997 return call;
2998 else
2999 {
3000 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(call), make_tmp, call);
3001 SET_EXPR_LOCATION(ret, location);
3002 return ret;
3003 }
3004 }
3005 }
3006
3007 // Return a tree for receiving a value of type TYPE_TREE on CHANNEL.
3008 // This does a blocking receive and returns the value read from the
3009 // channel. If FOR_SELECT is true, this is being done because it was
3010 // chosen in a select statement.
3011
3012 tree
3013 Gogo::receive_from_channel(tree type_tree, tree channel, bool for_select,
3014 source_location location)
3015 {
3016 if (type_tree == error_mark_node || channel == error_mark_node)
3017 return error_mark_node;
3018
3019 if (int_size_in_bytes(type_tree) <= 8
3020 && !AGGREGATE_TYPE_P(type_tree)
3021 && !FLOAT_TYPE_P(type_tree))
3022 {
3023 static tree receive_small_fndecl;
3024 tree call = Gogo::call_builtin(&receive_small_fndecl,
3025 location,
3026 "__go_receive_small",
3027 2,
3028 uint64_type_node,
3029 ptr_type_node,
3030 channel,
3031 boolean_type_node,
3032 (for_select
3033 ? boolean_true_node
3034 : boolean_false_node));
3035 if (call == error_mark_node)
3036 return error_mark_node;
3037 // This can panic if there are too many operations on a closed
3038 // channel.
3039 TREE_NOTHROW(receive_small_fndecl) = 0;
3040 int bitsize = GET_MODE_BITSIZE(TYPE_MODE(type_tree));
3041 tree int_type_tree = go_type_for_size(bitsize, 1);
3042 return fold_convert_loc(location, type_tree,
3043 fold_convert_loc(location, int_type_tree,
3044 call));
3045 }
3046 else
3047 {
3048 tree tmp = create_tmp_var(type_tree, get_name(type_tree));
3049 DECL_IGNORED_P(tmp) = 0;
3050 TREE_ADDRESSABLE(tmp) = 1;
3051 tree make_tmp = build1(DECL_EXPR, void_type_node, tmp);
3052 SET_EXPR_LOCATION(make_tmp, location);
3053 tree tmpaddr = build_fold_addr_expr(tmp);
3054 tmpaddr = fold_convert(ptr_type_node, tmpaddr);
3055 static tree receive_big_fndecl;
3056 tree call = Gogo::call_builtin(&receive_big_fndecl,
3057 location,
3058 "__go_receive_big",
3059 3,
3060 boolean_type_node,
3061 ptr_type_node,
3062 channel,
3063 ptr_type_node,
3064 tmpaddr,
3065 boolean_type_node,
3066 (for_select
3067 ? boolean_true_node
3068 : boolean_false_node));
3069 if (call == error_mark_node)
3070 return error_mark_node;
3071 // This can panic if there are too many operations on a closed
3072 // channel.
3073 TREE_NOTHROW(receive_big_fndecl) = 0;
3074 return build2(COMPOUND_EXPR, type_tree, make_tmp,
3075 build2(COMPOUND_EXPR, type_tree, call, tmp));
3076 }
3077 }
3078
3079 // Return the type of a function trampoline. This is like
3080 // get_trampoline_type in tree-nested.c.
3081
3082 tree
3083 Gogo::trampoline_type_tree()
3084 {
3085 static tree type_tree;
3086 if (type_tree == NULL_TREE)
3087 {
3088 unsigned int size;
3089 unsigned int align;
3090 go_trampoline_info(&size, &align);
3091 tree t = build_index_type(build_int_cst(integer_type_node, size - 1));
3092 t = build_array_type(char_type_node, t);
3093
3094 type_tree = Gogo::builtin_struct(NULL, "__go_trampoline", NULL_TREE, 1,
3095 "__data", t);
3096 t = TYPE_FIELDS(type_tree);
3097 DECL_ALIGN(t) = align;
3098 DECL_USER_ALIGN(t) = 1;
3099
3100 go_preserve_from_gc(type_tree);
3101 }
3102 return type_tree;
3103 }
3104
3105 // Make a trampoline which calls FNADDR passing CLOSURE.
3106
3107 tree
3108 Gogo::make_trampoline(tree fnaddr, tree closure, source_location location)
3109 {
3110 tree trampoline_type = Gogo::trampoline_type_tree();
3111 tree trampoline_size = TYPE_SIZE_UNIT(trampoline_type);
3112
3113 closure = save_expr(closure);
3114
3115 // We allocate the trampoline using a special function which will
3116 // mark it as executable.
3117 static tree trampoline_fndecl;
3118 tree x = Gogo::call_builtin(&trampoline_fndecl,
3119 location,
3120 "__go_allocate_trampoline",
3121 2,
3122 ptr_type_node,
3123 size_type_node,
3124 trampoline_size,
3125 ptr_type_node,
3126 fold_convert_loc(location, ptr_type_node,
3127 closure));
3128 if (x == error_mark_node)
3129 return error_mark_node;
3130
3131 x = save_expr(x);
3132
3133 // Initialize the trampoline.
3134 tree ini = build_call_expr(implicit_built_in_decls[BUILT_IN_INIT_TRAMPOLINE],
3135 3, x, fnaddr, closure);
3136
3137 // On some targets the trampoline address needs to be adjusted. For
3138 // example, when compiling in Thumb mode on the ARM, the address
3139 // needs to have the low bit set.
3140 x = build_call_expr(implicit_built_in_decls[BUILT_IN_ADJUST_TRAMPOLINE],
3141 1, x);
3142 x = fold_convert(TREE_TYPE(fnaddr), x);
3143
3144 return build2(COMPOUND_EXPR, TREE_TYPE(x), ini, x);
3145 }