Line data Source code
1 : // types.cc -- Go frontend types.
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 <ostream>
10 :
11 : #include "go-c.h"
12 : #include "gogo.h"
13 : #include "go-diagnostics.h"
14 : #include "go-encode-id.h"
15 : #include "go-sha1.h"
16 : #include "operator.h"
17 : #include "expressions.h"
18 : #include "statements.h"
19 : #include "export.h"
20 : #include "import.h"
21 : #include "backend.h"
22 : #include "types.h"
23 :
24 : // Forward declarations so that we don't have to make types.h #include
25 : // backend.h.
26 :
27 : static void
28 : get_backend_struct_fields(Gogo* gogo, Struct_type* type, bool use_placeholder,
29 : std::vector<Backend::Btyped_identifier>* bfields);
30 :
31 : static void
32 : get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
33 : std::vector<Backend::Btyped_identifier>* bfields);
34 :
35 : static void
36 : get_backend_interface_fields(Gogo* gogo, Interface_type* type,
37 : bool use_placeholder,
38 : std::vector<Backend::Btyped_identifier>* bfields);
39 :
40 : // Class Type.
41 :
42 13411460 : Type::Type(Type_classification classification)
43 13411460 : : classification_(classification), btype_(NULL), type_descriptor_var_(NULL),
44 13411460 : gc_symbol_var_(NULL)
45 : {
46 13411460 : }
47 :
48 20626 : Type::~Type()
49 : {
50 20626 : }
51 :
52 : // Get the base type for a type--skip names and forward declarations.
53 :
54 : Type*
55 796126563 : Type::base()
56 : {
57 820449830 : switch (this->classification_)
58 : {
59 103884666 : case TYPE_NAMED:
60 103884666 : return this->named_type()->named_base();
61 24323267 : case TYPE_FORWARD:
62 24323267 : return this->forward_declaration_type()->real_type()->base();
63 : default:
64 : return this;
65 : }
66 : }
67 :
68 : const Type*
69 565703285 : Type::base() const
70 : {
71 601081109 : switch (this->classification_)
72 : {
73 201639547 : case TYPE_NAMED:
74 201639547 : return this->named_type()->named_base();
75 35377824 : case TYPE_FORWARD:
76 35377824 : return this->forward_declaration_type()->real_type()->base();
77 : default:
78 : return this;
79 : }
80 : }
81 :
82 : // Skip defined forward declarations.
83 :
84 : Type*
85 243460761 : Type::forwarded()
86 : {
87 243460761 : Type* t = this;
88 243460761 : Forward_declaration_type* ftype = t->forward_declaration_type();
89 14638530 : while (ftype != NULL && ftype->is_defined())
90 : {
91 14626509 : t = ftype->real_type();
92 272713779 : ftype = t->forward_declaration_type();
93 : }
94 243460761 : return t;
95 : }
96 :
97 : const Type*
98 4087331845 : Type::forwarded() const
99 : {
100 4087331845 : const Type* t = this;
101 4087331845 : const Forward_declaration_type* ftype = t->forward_declaration_type();
102 248101629 : while (ftype != NULL && ftype->is_defined())
103 : {
104 247784616 : t = ftype->real_type();
105 4582901077 : ftype = t->forward_declaration_type();
106 : }
107 4087331845 : return t;
108 : }
109 :
110 : // Skip alias definitions.
111 :
112 : Type*
113 3995052 : Type::unalias()
114 : {
115 3995052 : Type* t = this->forwarded();
116 3995052 : Named_type* nt = t->named_type();
117 8117884 : while (nt != NULL && nt->is_alias())
118 : {
119 127780 : t = nt->real_type()->forwarded();
120 127780 : nt = t->named_type();
121 : }
122 3995052 : return t;
123 : }
124 :
125 : const Type*
126 195770593 : Type::unalias() const
127 : {
128 195770593 : const Type* t = this->forwarded();
129 195770593 : const Named_type* nt = t->named_type();
130 408447265 : while (nt != NULL && nt->is_alias())
131 : {
132 16906079 : t = nt->real_type()->forwarded();
133 16906079 : nt = t->named_type();
134 : }
135 195770593 : return t;
136 : }
137 :
138 : // If this is a named type, return it. Otherwise, return NULL.
139 :
140 : Named_type*
141 214565384 : Type::named_type()
142 : {
143 214565384 : return this->forwarded()->convert_no_base<Named_type, TYPE_NAMED>();
144 : }
145 :
146 : const Named_type*
147 1464041543 : Type::named_type() const
148 : {
149 1464041543 : return this->forwarded()->convert_no_base<const Named_type, TYPE_NAMED>();
150 : }
151 :
152 : // Return true if this type is not defined.
153 :
154 : bool
155 12780712 : Type::is_undefined() const
156 : {
157 12780712 : return this->forwarded()->forward_declaration_type() != NULL;
158 : }
159 :
160 : // Return true if this is a basic type: a type which is not composed
161 : // of other types, and is not void.
162 :
163 : bool
164 1371079 : Type::is_basic_type() const
165 : {
166 1371103 : switch (this->classification_)
167 : {
168 : case TYPE_INTEGER:
169 : case TYPE_FLOAT:
170 : case TYPE_COMPLEX:
171 : case TYPE_BOOLEAN:
172 : case TYPE_STRING:
173 : case TYPE_NIL:
174 : return true;
175 :
176 1371078 : case TYPE_ERROR:
177 1371078 : case TYPE_VOID:
178 1371078 : case TYPE_FUNCTION:
179 1371078 : case TYPE_POINTER:
180 1371078 : case TYPE_STRUCT:
181 1371078 : case TYPE_ARRAY:
182 1371078 : case TYPE_MAP:
183 1371078 : case TYPE_CHANNEL:
184 1371078 : case TYPE_INTERFACE:
185 1371078 : return false;
186 :
187 24 : case TYPE_NAMED:
188 24 : case TYPE_FORWARD:
189 24 : return this->base()->is_basic_type();
190 :
191 0 : default:
192 0 : go_unreachable();
193 : }
194 : }
195 :
196 : // Return true if this is an abstract type.
197 :
198 : bool
199 32333315 : Type::is_abstract() const
200 : {
201 32333315 : switch (this->classification())
202 : {
203 1793156 : case TYPE_INTEGER:
204 3586312 : return this->integer_type()->is_abstract();
205 42753 : case TYPE_FLOAT:
206 85506 : return this->float_type()->is_abstract();
207 10082 : case TYPE_COMPLEX:
208 20164 : return this->complex_type()->is_abstract();
209 : case TYPE_STRING:
210 : return this->is_abstract_string_type();
211 : case TYPE_BOOLEAN:
212 : return this->is_abstract_boolean_type();
213 : default:
214 : return false;
215 : }
216 : }
217 :
218 : // Return a non-abstract version of an abstract type.
219 :
220 : Type*
221 42437 : Type::make_non_abstract_type()
222 : {
223 42437 : go_assert(this->is_abstract());
224 42437 : switch (this->classification())
225 : {
226 29918 : case TYPE_INTEGER:
227 59836 : if (this->integer_type()->is_rune())
228 334 : return Type::lookup_integer_type("int32");
229 : else
230 29584 : return Type::lookup_integer_type("int");
231 256 : case TYPE_FLOAT:
232 256 : return Type::lookup_float_type("float64");
233 179 : case TYPE_COMPLEX:
234 179 : return Type::lookup_complex_type("complex128");
235 3569 : case TYPE_STRING:
236 3569 : return Type::lookup_string_type();
237 8515 : case TYPE_BOOLEAN:
238 8515 : return Type::lookup_bool_type();
239 0 : default:
240 0 : go_unreachable();
241 : }
242 : }
243 :
244 : // Return true if this is an error type. Don't give an error if we
245 : // try to dereference an undefined forwarding type, as this is called
246 : // in the parser when the type may legitimately be undefined.
247 :
248 : bool
249 2227303200 : Type::is_error_type() const
250 : {
251 2227303200 : const Type* t = this->forwarded();
252 : // Note that we return false for an undefined forward type.
253 2227303200 : switch (t->classification_)
254 : {
255 : case TYPE_ERROR:
256 : return true;
257 882472144 : case TYPE_NAMED:
258 882472144 : return t->named_type()->is_named_error_type();
259 1344813942 : default:
260 1344813942 : return false;
261 : }
262 : }
263 :
264 : // Note that this type is an error. This is called by children when
265 : // they discover an error during the verify_types pass.
266 :
267 : void
268 47 : Type::set_is_error()
269 : {
270 47 : this->classification_ = TYPE_ERROR;
271 47 : }
272 :
273 : // Return a string version of this type to use in an error message.
274 :
275 : std::string
276 211438 : Type::message_name() const
277 : {
278 211438 : std::string ret;
279 211438 : this->do_message_name(&ret);
280 211438 : return ret;
281 : }
282 :
283 : // If this is a pointer type, return the type to which it points.
284 : // Otherwise, return NULL.
285 :
286 : Type*
287 108671671 : Type::points_to() const
288 : {
289 108671671 : const Pointer_type* ptype = this->convert<const Pointer_type,
290 185036329 : TYPE_POINTER>();
291 76364658 : return ptype == NULL ? NULL : ptype->points_to();
292 : }
293 :
294 : // Return whether this is a slice type.
295 :
296 : bool
297 57200563 : Type::is_slice_type() const
298 : {
299 77735611 : return this->array_type() != NULL && this->array_type()->length() == NULL;
300 : }
301 :
302 : // Return whether this is the predeclared constant nil being used as a
303 : // type.
304 :
305 : bool
306 1204577 : Type::is_nil_constant_as_type() const
307 : {
308 1204577 : const Type* t = this->forwarded();
309 1204577 : if (t->forward_declaration_type() != NULL)
310 : {
311 1302 : const Named_object* no = t->forward_declaration_type()->named_object();
312 1302 : if (no->is_unknown())
313 16 : no = no->unknown_value()->real_named_object();
314 16 : if (no != NULL
315 1286 : && no->is_const()
316 1292 : && no->const_value()->expr()->is_nil_expression())
317 : return true;
318 : }
319 : return false;
320 : }
321 :
322 : // Traverse a type.
323 :
324 : int
325 1186061928 : Type::traverse(Type* type, Traverse* traverse)
326 : {
327 1186061928 : go_assert((traverse->traverse_mask() & Traverse::traverse_types) != 0
328 : || (traverse->traverse_mask()
329 : & Traverse::traverse_expressions) != 0);
330 1186061928 : if (traverse->remember_type(type))
331 : {
332 : // We have already traversed this type.
333 : return TRAVERSE_CONTINUE;
334 : }
335 678335632 : if ((traverse->traverse_mask() & Traverse::traverse_types) != 0)
336 : {
337 73710495 : int t = traverse->type(type);
338 73710495 : if (t == TRAVERSE_EXIT)
339 : return TRAVERSE_EXIT;
340 73710479 : else if (t == TRAVERSE_SKIP_COMPONENTS)
341 : return TRAVERSE_CONTINUE;
342 : }
343 : // An array type has an expression which we need to traverse if
344 : // traverse_expressions is set.
345 674317710 : if (type->do_traverse(traverse) == TRAVERSE_EXIT)
346 : return TRAVERSE_EXIT;
347 : return TRAVERSE_CONTINUE;
348 : }
349 :
350 : // Default implementation for do_traverse for child class.
351 :
352 : int
353 65026152 : Type::do_traverse(Traverse*)
354 : {
355 65026152 : return TRAVERSE_CONTINUE;
356 : }
357 :
358 : // Return whether two types are identical. If REASON is not NULL,
359 : // optionally set *REASON to the reason the types are not identical.
360 :
361 : bool
362 67163888 : Type::are_identical(const Type* t1, const Type* t2, int flags,
363 : std::string* reason)
364 : {
365 70281646 : if (t1 == NULL || t2 == NULL)
366 : {
367 : // Something is wrong.
368 0 : return (flags & COMPARE_ERRORS) == 0 ? true : t1 == t2;
369 : }
370 :
371 : // Skip defined forward declarations.
372 70281646 : t1 = t1->forwarded();
373 70281646 : t2 = t2->forwarded();
374 :
375 70281646 : if ((flags & COMPARE_ALIASES) == 0)
376 : {
377 : // Ignore aliases.
378 61130620 : t1 = t1->unalias();
379 61130620 : t2 = t2->unalias();
380 : }
381 :
382 70281646 : if (t1 == t2)
383 : return true;
384 :
385 : // An undefined forward declaration is an error.
386 24904738 : if (t1->forward_declaration_type() != NULL
387 24904738 : || t2->forward_declaration_type() != NULL)
388 12 : return (flags & COMPARE_ERRORS) == 0;
389 :
390 : // Avoid cascading errors with error types.
391 24904726 : if (t1->is_error_type() || t2->is_error_type())
392 : {
393 3002 : if ((flags & COMPARE_ERRORS) == 0)
394 : return true;
395 178 : return t1->is_error_type() && t2->is_error_type();
396 : }
397 :
398 : // Get a good reason for the sink type. Note that the sink type on
399 : // the left hand side of an assignment is handled in are_assignable.
400 49797029 : if (t1->is_sink_type() || t2->is_sink_type())
401 : {
402 6419 : if (reason != NULL)
403 0 : *reason = "invalid use of _";
404 6419 : return false;
405 : }
406 :
407 : // A named type is only identical to itself.
408 24895305 : if (t1->named_type() != NULL || t2->named_type() != NULL)
409 3396212 : return false;
410 :
411 : // Check type shapes.
412 21499093 : if (t1->classification() != t2->classification())
413 : return false;
414 :
415 18458338 : switch (t1->classification())
416 : {
417 : case TYPE_VOID:
418 : case TYPE_BOOLEAN:
419 : case TYPE_STRING:
420 : case TYPE_NIL:
421 : // These types are always identical.
422 : return true;
423 :
424 287752 : case TYPE_INTEGER:
425 863256 : return t1->integer_type()->is_identical(t2->integer_type());
426 :
427 1358 : case TYPE_FLOAT:
428 4074 : return t1->float_type()->is_identical(t2->float_type());
429 :
430 162 : case TYPE_COMPLEX:
431 486 : return t1->complex_type()->is_identical(t2->complex_type());
432 :
433 453251 : case TYPE_FUNCTION:
434 1359753 : return t1->function_type()->is_identical(t2->function_type(),
435 453251 : false, flags, reason);
436 :
437 3117758 : case TYPE_POINTER:
438 3117758 : return Type::are_identical(t1->points_to(), t2->points_to(), flags,
439 3117758 : reason);
440 :
441 495451 : case TYPE_STRUCT:
442 1486353 : return t1->struct_type()->is_identical(t2->struct_type(), flags);
443 :
444 13923609 : case TYPE_ARRAY:
445 41770827 : return t1->array_type()->is_identical(t2->array_type(), flags);
446 :
447 42492 : case TYPE_MAP:
448 127476 : return t1->map_type()->is_identical(t2->map_type(), flags);
449 :
450 26351 : case TYPE_CHANNEL:
451 79053 : return t1->channel_type()->is_identical(t2->channel_type(), flags);
452 :
453 110154 : case TYPE_INTERFACE:
454 330462 : return t1->interface_type()->is_identical(t2->interface_type(), flags);
455 :
456 0 : case TYPE_CALL_MULTIPLE_RESULT:
457 0 : if (reason != NULL)
458 0 : *reason = "invalid use of multiple-value function call";
459 : return false;
460 :
461 0 : default:
462 0 : go_unreachable();
463 : }
464 : }
465 :
466 : // Return true if it's OK to have a binary operation with types LHS
467 : // and RHS. This is not used for shifts or comparisons.
468 :
469 : bool
470 298378 : Type::are_compatible_for_binop(const Type* lhs, const Type* rhs)
471 : {
472 298378 : if (Type::are_identical(lhs, rhs, Type::COMPARE_TAGS, NULL))
473 : return true;
474 :
475 : // A constant of abstract bool type may be mixed with any bool type.
476 561 : if ((rhs->is_abstract_boolean_type() && lhs->is_boolean_type())
477 561 : || (lhs->is_abstract_boolean_type() && rhs->is_boolean_type()))
478 0 : return true;
479 :
480 : // A constant of abstract string type may be mixed with any string
481 : // type.
482 561 : if ((rhs->is_abstract_string_type() && lhs->is_string_type())
483 563 : || (lhs->is_abstract_string_type() && rhs->is_string_type()))
484 0 : return true;
485 :
486 560 : lhs = lhs->base();
487 560 : rhs = rhs->base();
488 :
489 : // A constant of abstract integer, float, or complex type may be
490 : // mixed with an integer, float, or complex type.
491 560 : if ((rhs->is_abstract()
492 753 : && (rhs->integer_type() != NULL
493 719 : || rhs->float_type() != NULL
494 505 : || rhs->complex_type() != NULL)
495 295 : && (lhs->integer_type() != NULL
496 2 : || lhs->float_type() != NULL
497 601 : || lhs->complex_type() != NULL))
498 576 : || (lhs->is_abstract()
499 57 : && (lhs->integer_type() != NULL
500 57 : || lhs->float_type() != NULL
501 43 : || lhs->complex_type() != NULL)
502 2 : && (rhs->integer_type() != NULL
503 546 : || rhs->float_type() != NULL
504 544 : || rhs->complex_type() != NULL)))
505 544 : return true;
506 :
507 : // The nil type may be compared to a pointer, an interface type, a
508 : // slice type, a channel type, a map type, or a function type.
509 16 : if (lhs->is_nil_type()
510 16 : && (rhs->points_to() != NULL
511 0 : || rhs->interface_type() != NULL
512 0 : || rhs->is_slice_type()
513 0 : || rhs->map_type() != NULL
514 0 : || rhs->channel_type() != NULL
515 0 : || rhs->function_type() != NULL))
516 0 : return true;
517 16 : if (rhs->is_nil_type()
518 16 : && (lhs->points_to() != NULL
519 0 : || lhs->interface_type() != NULL
520 0 : || lhs->is_slice_type()
521 0 : || lhs->map_type() != NULL
522 0 : || lhs->channel_type() != NULL
523 0 : || lhs->function_type() != NULL))
524 0 : return true;
525 :
526 : return false;
527 : }
528 :
529 : // Return true if a value with type T1 may be compared with a value of
530 : // type T2. IS_EQUALITY_OP is true for == or !=, false for <, etc.
531 :
532 : bool
533 14316771 : Type::are_compatible_for_comparison(bool is_equality_op, const Type *t1,
534 : const Type *t2, std::string *reason)
535 : {
536 14316771 : if (t1 != t2
537 257174 : && !Type::are_assignable(t1, t2, NULL)
538 14395474 : && !Type::are_assignable(t2, t1, NULL))
539 : {
540 4 : if (reason != NULL)
541 2 : *reason = "incompatible types in binary expression";
542 4 : return false;
543 : }
544 :
545 14316767 : if (!is_equality_op)
546 : {
547 158362 : if (t1->integer_type() == NULL
548 1014 : && t1->float_type() == NULL
549 1014 : && !t1->is_string_type())
550 : {
551 20 : if (reason != NULL)
552 20 : *reason = _("invalid comparison of non-ordered type");
553 20 : return false;
554 : }
555 : }
556 14163883 : else if (t1->is_slice_type()
557 13822183 : || t1->map_type() != NULL
558 14172075 : || t1->function_type() != NULL
559 13680997 : || t2->is_slice_type()
560 13676456 : || t2->map_type() != NULL
561 14330183 : || t2->function_type() != NULL)
562 : {
563 491078 : if (!t1->is_nil_type() && !t2->is_nil_type())
564 : {
565 474697 : if (reason != NULL)
566 : {
567 33 : if (t1->is_slice_type() || t2->is_slice_type())
568 7 : *reason = _("slice can only be compared to nil");
569 46 : else if (t1->map_type() != NULL || t2->map_type() != NULL)
570 7 : *reason = _("map can only be compared to nil");
571 : else
572 19 : *reason = _("func can only be compared to nil");
573 :
574 : // Match 6g error messages.
575 57 : if (t1->interface_type() != NULL || t2->interface_type() != NULL)
576 : {
577 18 : char buf[200];
578 18 : snprintf(buf, sizeof buf, _("invalid operation (%s)"),
579 : reason->c_str());
580 18 : *reason = buf;
581 : }
582 : }
583 474697 : return false;
584 : }
585 : }
586 : else
587 : {
588 13672805 : if (!t1->is_boolean_type()
589 6216189 : && t1->integer_type() == NULL
590 19799890 : && t1->float_type() == NULL
591 19741082 : && t1->complex_type() == NULL
592 6068298 : && !t1->is_string_type()
593 5033723 : && t1->points_to() == NULL
594 17858154 : && t1->channel_type() == NULL
595 17137665 : && t1->interface_type() == NULL
596 14523274 : && t1->struct_type() == NULL
597 13672805 : && t1->array_type() == NULL
598 13741478 : && !t1->is_nil_type())
599 : {
600 21 : if (reason != NULL)
601 0 : *reason = _("invalid comparison of non-comparable type");
602 21 : return false;
603 : }
604 :
605 13672784 : if (t1->unalias()->named_type() != NULL)
606 6140581 : return t1->unalias()->named_type()->named_type_is_comparable(reason);
607 7532203 : else if (t2->unalias()->named_type() != NULL)
608 62628 : return t2->unalias()->named_type()->named_type_is_comparable(reason);
609 7469575 : else if (t1->struct_type() != NULL)
610 : {
611 3008970 : if (t1->struct_type()->is_struct_incomparable())
612 : {
613 189656 : if (reason != NULL)
614 0 : *reason = _("invalid comparison of generated struct");
615 189656 : return false;
616 : }
617 2629658 : const Struct_field_list* fields = t1->struct_type()->fields();
618 5607051 : for (Struct_field_list::const_iterator p = fields->begin();
619 5607051 : p != fields->end();
620 4292222 : ++p)
621 : {
622 4443701 : if (!p->type()->is_comparable())
623 : {
624 151479 : if (reason != NULL)
625 3 : *reason = _("invalid comparison of non-comparable struct");
626 151479 : return false;
627 : }
628 : }
629 : }
630 5965090 : else if (t1->array_type() != NULL)
631 : {
632 1451254 : if (t1->array_type()->is_array_incomparable())
633 : {
634 122421 : if (reason != NULL)
635 0 : *reason = _("invalid comparison of generated array");
636 122421 : return false;
637 : }
638 1206412 : if (t1->array_type()->length()->is_nil_expression()
639 1206412 : || !t1->array_type()->element_type()->is_comparable())
640 : {
641 4286 : if (reason != NULL)
642 2 : *reason = _("invalid comparison of non-comparable array");
643 4286 : return false;
644 : }
645 : }
646 : }
647 :
648 : return true;
649 : }
650 :
651 : // Return true if a value with type RHS may be assigned to a variable
652 : // with type LHS. If REASON is not NULL, set *REASON to the reason
653 : // the types are not assignable.
654 :
655 : bool
656 5358698 : Type::are_assignable(const Type* lhs, const Type* rhs, std::string* reason)
657 : {
658 : // Do some checks first. Make sure the types are defined.
659 5358698 : if (rhs != NULL && !rhs->is_undefined())
660 : {
661 5358694 : if (rhs->is_void_type())
662 : {
663 1 : if (reason != NULL)
664 1 : *reason = "non-value used as value";
665 1 : return false;
666 : }
667 5358693 : if (rhs->is_call_multiple_result_type())
668 : {
669 8 : if (reason != NULL)
670 8 : reason->assign(_("multiple-value function call in "
671 : "single-value context"));
672 8 : return false;
673 : }
674 : }
675 :
676 : // Any value may be assigned to the blank identifier.
677 5358689 : if (lhs != NULL
678 5358689 : && !lhs->is_undefined()
679 10717373 : && lhs->is_sink_type())
680 : return true;
681 :
682 : // Identical types are assignable.
683 5331091 : if (Type::are_identical(lhs, rhs, Type::COMPARE_TAGS, reason))
684 : return true;
685 :
686 : // Ignore aliases, except for error messages.
687 1229448 : const Type* lhs_orig = lhs;
688 1229448 : const Type* rhs_orig = rhs;
689 1229448 : lhs = lhs->unalias();
690 1229448 : rhs = rhs->unalias();
691 :
692 : // The types are assignable if they have identical underlying types
693 : // and either LHS or RHS is not a named type.
694 1994643 : if (((lhs->named_type() != NULL && rhs->named_type() == NULL)
695 790511 : || (rhs->named_type() != NULL && lhs->named_type() == NULL))
696 1949014 : && Type::are_identical(lhs->base(), rhs->base(), Type::COMPARE_TAGS,
697 : reason))
698 : return true;
699 :
700 : // The types are assignable if LHS is an interface type and RHS
701 : // implements the required methods.
702 1196045 : const Interface_type* lhs_interface_type = lhs->interface_type();
703 481605 : if (lhs_interface_type != NULL)
704 : {
705 481605 : if (lhs_interface_type->implements_interface(rhs, reason))
706 : return true;
707 179050 : const Interface_type* rhs_interface_type = rhs->interface_type();
708 4508 : if (rhs_interface_type != NULL
709 4508 : && lhs_interface_type->is_compatible_for_assign(rhs_interface_type,
710 : reason))
711 : return true;
712 : }
713 :
714 : // The type are assignable if RHS is a bidirectional channel type,
715 : // LHS is a channel type, they have identical element types, and
716 : // either LHS or RHS is not a named type.
717 891117 : if (lhs->channel_type() != NULL
718 757 : && rhs->channel_type() != NULL
719 757 : && rhs->channel_type()->may_send()
720 717 : && rhs->channel_type()->may_receive()
721 697 : && (lhs->named_type() == NULL || rhs->named_type() == NULL)
722 691 : && Type::are_identical(lhs->channel_type()->element_type(),
723 691 : rhs->channel_type()->element_type(),
724 : Type::COMPARE_TAGS,
725 : reason))
726 : return true;
727 :
728 : // The nil type may be assigned to a pointer, function, slice, map,
729 : // channel, or interface type.
730 888386 : if (rhs->is_nil_type()
731 888386 : && (lhs->points_to() != NULL
732 500256 : || lhs->function_type() != NULL
733 209388 : || lhs->is_slice_type()
734 466653 : || lhs->map_type() != NULL
735 465381 : || lhs->channel_type() != NULL
736 290868 : || lhs->interface_type() != NULL))
737 290868 : return true;
738 :
739 : // An untyped numeric constant may be assigned to a numeric type if
740 : // it is representable in that type.
741 597518 : if ((rhs->is_abstract()
742 23776 : && (rhs->integer_type() != NULL
743 23730 : || rhs->float_type() != NULL
744 23645 : || rhs->complex_type() != NULL))
745 597625 : && (lhs->integer_type() != NULL
746 23694 : || lhs->float_type() != NULL
747 23633 : || lhs->complex_type() != NULL))
748 23633 : return true;
749 :
750 : // Give some better error messages.
751 573885 : if (reason != NULL && reason->empty())
752 : {
753 105705 : if (rhs->interface_type() != NULL)
754 3 : reason->assign(_("need explicit conversion"));
755 : else
756 : {
757 105702 : const std::string& lhs_name(lhs_orig->message_name());
758 105702 : const std::string& rhs_name(rhs_orig->message_name());
759 105702 : size_t len = lhs_name.length() + rhs_name.length() + 100;
760 105702 : char* buf = new char[len];
761 105702 : snprintf(buf, len, _("cannot use type %s as type %s"),
762 : rhs_name.c_str(), lhs_name.c_str());
763 105702 : reason->assign(buf);
764 105702 : delete[] buf;
765 105702 : }
766 : }
767 :
768 : return false;
769 : }
770 :
771 : // Return true if a value with type RHS may be converted to type LHS.
772 : // If REASON is not NULL, set *REASON to the reason the types are not
773 : // convertible.
774 :
775 : bool
776 538636 : Type::are_convertible(const Type* lhs, const Type* rhs, std::string* reason)
777 : {
778 : // The types are convertible if they are assignable.
779 538636 : if (Type::are_assignable(lhs, rhs, reason))
780 : return true;
781 :
782 : // Ignore aliases.
783 494733 : lhs = lhs->unalias();
784 494733 : rhs = rhs->unalias();
785 :
786 : // A pointer to a regular type may not be converted to a pointer to
787 : // a type that may not live in the heap, except when converting from
788 : // unsafe.Pointer.
789 494733 : if (lhs->points_to() != NULL
790 14629 : && rhs->points_to() != NULL
791 13651 : && !lhs->points_to()->in_heap()
792 408 : && rhs->points_to()->in_heap()
793 495134 : && !rhs->is_unsafe_pointer_type())
794 : {
795 0 : if (reason != NULL)
796 0 : reason->assign(_("conversion from normal type to notinheap type"));
797 0 : return false;
798 : }
799 :
800 : // The types are convertible if they have identical underlying
801 : // types, ignoring struct field tags.
802 494733 : if (Type::are_identical(lhs->base(), rhs->base(), 0, reason))
803 : return true;
804 :
805 : // The types are convertible if they are both unnamed pointer types
806 : // and their pointer base types have identical underlying types,
807 : // ignoring struct field tags.
808 425624 : if (lhs->named_type() == NULL
809 13176 : && rhs->named_type() == NULL
810 1055 : && lhs->points_to() != NULL
811 1016 : && rhs->points_to() != NULL
812 972 : && (lhs->points_to()->named_type() != NULL
813 27 : || rhs->points_to()->named_type() != NULL)
814 426596 : && Type::are_identical(lhs->points_to()->base(),
815 972 : rhs->points_to()->base(),
816 : 0, reason))
817 : return true;
818 :
819 : // Integer and floating point types are convertible to each other.
820 854112 : if ((lhs->integer_type() != NULL || lhs->float_type() != NULL)
821 402145 : && (rhs->integer_type() != NULL || rhs->float_type() != NULL))
822 228189 : return true;
823 :
824 : // Complex types are convertible to each other.
825 735114 : if (lhs->complex_type() != NULL && rhs->complex_type() != NULL)
826 : return true;
827 :
828 : // An integer, or []byte, or []rune, may be converted to a string.
829 196350 : if (lhs->is_string_type())
830 : {
831 543783 : if (rhs->integer_type() != NULL)
832 : return true;
833 4629 : if (rhs->is_slice_type())
834 : {
835 9246 : const Type* e = rhs->array_type()->element_type()->forwarded();
836 9246 : if (e->integer_type() != NULL
837 4623 : && (e->integer_type()->is_byte()
838 136 : || e->integer_type()->is_rune()))
839 4619 : return true;
840 : }
841 : }
842 :
843 : // A string may be converted to []byte or []rune.
844 191111 : if (rhs->is_string_type() && lhs->is_slice_type())
845 : {
846 12894 : const Type* e = lhs->array_type()->element_type()->forwarded();
847 12894 : if (e->integer_type() != NULL
848 6550 : && (e->integer_type()->is_byte() || e->integer_type()->is_rune()))
849 6443 : return true;
850 : }
851 :
852 : // A slice may be converted to a pointer-to-array.
853 184668 : if (rhs->is_slice_type()
854 82 : && lhs->points_to() != NULL
855 46 : && lhs->points_to()->array_type() != NULL
856 46 : && !lhs->points_to()->is_slice_type()
857 184760 : && Type::are_identical(lhs->points_to()->array_type()->element_type(),
858 46 : rhs->array_type()->element_type(), 0, reason))
859 : return true;
860 :
861 : // An unsafe.Pointer type may be converted to any pointer type or to
862 : // a type whose underlying type is uintptr, and vice-versa.
863 184622 : if (lhs->is_unsafe_pointer_type()
864 184622 : && (rhs->points_to() != NULL
865 932 : || (rhs->integer_type() != NULL
866 932 : && rhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
867 7556 : return true;
868 177066 : if (rhs->is_unsafe_pointer_type()
869 177066 : && (lhs->points_to() != NULL
870 170919 : || (lhs->integer_type() != NULL
871 170919 : && lhs->integer_type() == Type::lookup_integer_type("uintptr")->real_type())))
872 176964 : return true;
873 :
874 : // Give a better error message.
875 102 : if (reason != NULL)
876 : {
877 102 : if (reason->empty())
878 0 : *reason = "invalid type conversion";
879 : else
880 : {
881 102 : std::string s = "invalid type conversion (";
882 102 : s += *reason;
883 102 : s += ')';
884 102 : *reason = s;
885 102 : }
886 : }
887 :
888 : return false;
889 : }
890 :
891 : // Copy expressions if it may change the size.
892 : //
893 : // The only type that has an expression is an array type. The only
894 : // types whose size can be changed by the size of an array type are an
895 : // array type itself, or a struct type with an array field.
896 : Type*
897 469204 : Type::copy_expressions()
898 : {
899 : // This is run during parsing, so types may not be valid yet.
900 : // We only have to worry about array type literals.
901 469204 : switch (this->classification_)
902 : {
903 : default:
904 : return this;
905 :
906 3 : case TYPE_ARRAY:
907 3 : {
908 3 : Array_type* at = this->array_type();
909 3 : if (at->length() == NULL)
910 : return this;
911 3 : Expression* len = at->length()->copy();
912 3 : if (at->length() == len)
913 : return this;
914 3 : return Type::make_array_type(at->element_type(), len);
915 : }
916 :
917 0 : case TYPE_STRUCT:
918 0 : {
919 0 : Struct_type* st = this->struct_type();
920 0 : const Struct_field_list* sfl = st->fields();
921 0 : if (sfl == NULL)
922 : return this;
923 0 : bool changed = false;
924 0 : Struct_field_list *nsfl = new Struct_field_list();
925 0 : for (Struct_field_list::const_iterator pf = sfl->begin();
926 0 : pf != sfl->end();
927 0 : ++pf)
928 : {
929 0 : Type* ft = pf->type()->copy_expressions();
930 0 : Struct_field nf(Typed_identifier((pf->is_anonymous()
931 0 : ? ""
932 0 : : pf->field_name()),
933 : ft,
934 0 : pf->location()));
935 0 : if (pf->has_tag())
936 0 : nf.set_tag(pf->tag());
937 0 : nsfl->push_back(nf);
938 0 : if (ft != pf->type())
939 0 : changed = true;
940 0 : }
941 0 : if (!changed)
942 : {
943 0 : delete(nsfl);
944 0 : return this;
945 : }
946 0 : return Type::make_struct_type(nsfl, st->location());
947 : }
948 : }
949 :
950 : go_unreachable();
951 : }
952 :
953 : // Return a hash code for the type to be used for method lookup.
954 :
955 : unsigned int
956 22982827 : Type::hash_for_method(Gogo* gogo, int flags) const
957 : {
958 22982827 : const Type* t = this->forwarded();
959 22982827 : if (t->named_type() != NULL && t->named_type()->is_alias())
960 : {
961 966525 : unsigned int r =
962 966525 : t->named_type()->real_type()->hash_for_method(gogo, flags);
963 966525 : if ((flags & Type::COMPARE_ALIASES) != 0)
964 214413 : r += TYPE_FORWARD;
965 966525 : return r;
966 : }
967 22016302 : unsigned int ret = t->classification_;
968 22016302 : return ret + t->do_hash_for_method(gogo, flags);
969 : }
970 :
971 : // Default implementation of do_hash_for_method. This is appropriate
972 : // for types with no subfields.
973 :
974 : unsigned int
975 937468 : Type::do_hash_for_method(Gogo*, int) const
976 : {
977 937468 : return 0;
978 : }
979 :
980 : // A hash table mapping unnamed types to the backend representation of
981 : // those types.
982 :
983 : Type::Type_btypes Type::type_btypes;
984 :
985 : // Return the backend representation for this type.
986 :
987 : Btype*
988 61865459 : Type::get_backend(Gogo* gogo)
989 : {
990 61865459 : if (this->btype_ != NULL)
991 : return this->btype_;
992 :
993 5916904 : if (this->named_type() != NULL && this->named_type()->is_alias())
994 : {
995 49356 : Btype* bt = this->unalias()->get_backend(gogo);
996 49356 : if (gogo != NULL && gogo->named_types_are_converted())
997 44710 : this->btype_ = bt;
998 49356 : return bt;
999 : }
1000 :
1001 5867548 : if (this->forward_declaration_type() != NULL
1002 5326404 : || this->named_type() != NULL)
1003 3781932 : return this->get_btype_without_hash(gogo);
1004 :
1005 2085616 : if (this->is_error_type())
1006 22 : return gogo->backend()->error_type();
1007 :
1008 : // To avoid confusing the backend, translate all identical Go types
1009 : // to the same backend representation. We use a hash table to do
1010 : // that. There is no need to use the hash table for named types, as
1011 : // named types are only identical to themselves.
1012 :
1013 2085594 : std::pair<Type*, Type_btype_entry> val;
1014 2085594 : val.first = this;
1015 2085594 : val.second.btype = NULL;
1016 2085594 : val.second.is_placeholder = false;
1017 2085594 : std::pair<Type_btypes::iterator, bool> ins =
1018 2085594 : Type::type_btypes.insert(val);
1019 2085594 : if (!ins.second && ins.first->second.btype != NULL)
1020 : {
1021 : // Note that GOGO can be NULL here, but only when the GCC
1022 : // middle-end is asking for a frontend type. That will only
1023 : // happen for simple types, which should never require
1024 : // placeholders.
1025 1278376 : if (!ins.first->second.is_placeholder)
1026 1024981 : this->btype_ = ins.first->second.btype;
1027 253395 : else if (gogo->named_types_are_converted())
1028 : {
1029 230753 : this->finish_backend(gogo, ins.first->second.btype);
1030 230753 : ins.first->second.is_placeholder = false;
1031 : }
1032 :
1033 : // We set the has_padding field of a Struct_type when we convert
1034 : // to the backend type, so if we have multiple Struct_type's
1035 : // mapping to the same backend type we need to copy the
1036 : // has_padding field. FIXME: This is awkward. We shouldn't
1037 : // really change the type when setting the backend type, but
1038 : // there isn't any other good time to add the padding field.
1039 1457312 : if (ins.first->first->struct_type() != NULL
1040 357872 : && ins.first->first->struct_type()->has_padding())
1041 32 : this->struct_type()->set_has_padding();
1042 :
1043 1278376 : return ins.first->second.btype;
1044 : }
1045 :
1046 807218 : Btype* bt = this->get_btype_without_hash(gogo);
1047 :
1048 807218 : if (ins.first->second.btype == NULL)
1049 : {
1050 805648 : ins.first->second.btype = bt;
1051 805648 : ins.first->second.is_placeholder = false;
1052 : }
1053 : else
1054 : {
1055 : // We have already created a backend representation for this
1056 : // type. This can happen when an unnamed type is defined using
1057 : // a named type which in turns uses an identical unnamed type.
1058 : // Use the representation we created earlier and ignore the one we just
1059 : // built.
1060 1570 : if (this->btype_ == bt)
1061 1570 : this->btype_ = ins.first->second.btype;
1062 : bt = ins.first->second.btype;
1063 : }
1064 :
1065 : return bt;
1066 : }
1067 :
1068 : // Return the backend representation for a type without looking in the
1069 : // hash table for identical types. This is used for named types,
1070 : // since a named type is never identical to any other type.
1071 :
1072 : Btype*
1073 5339557 : Type::get_btype_without_hash(Gogo* gogo)
1074 : {
1075 5339557 : if (this->btype_ == NULL)
1076 : {
1077 5053076 : Btype* bt = this->do_get_backend(gogo);
1078 :
1079 : // For a recursive function or pointer type, we will temporarily
1080 : // return a circular pointer type during the recursion. We
1081 : // don't want to record that for a forwarding type, as it may
1082 : // confuse us later.
1083 5053076 : if (this->forward_declaration_type() != NULL
1084 541144 : && gogo->backend()->is_circular_pointer_type(bt))
1085 : return bt;
1086 :
1087 5053045 : if (gogo == NULL || !gogo->named_types_are_converted())
1088 : return bt;
1089 :
1090 1705168 : this->btype_ = bt;
1091 : }
1092 1991649 : return this->btype_;
1093 : }
1094 :
1095 : // Get the backend representation of a type without forcing the
1096 : // creation of the backend representation of all supporting types.
1097 : // This will return a backend type that has the correct size but may
1098 : // be incomplete. E.g., a pointer will just be a placeholder pointer,
1099 : // and will not contain the final representation of the type to which
1100 : // it points. This is used while converting all named types to the
1101 : // backend representation, to avoid problems with indirect references
1102 : // to types which are not yet complete. When this is called, the
1103 : // sizes of all direct references (e.g., a struct field) should be
1104 : // known, but the sizes of indirect references (e.g., the type to
1105 : // which a pointer points) may not.
1106 :
1107 : Btype*
1108 17501276 : Type::get_backend_placeholder(Gogo* gogo)
1109 : {
1110 17542517 : if (gogo->named_types_are_converted())
1111 13804933 : return this->get_backend(gogo);
1112 3737584 : if (this->btype_ != NULL)
1113 : return this->btype_;
1114 :
1115 3719084 : Btype* bt;
1116 3719084 : switch (this->classification_)
1117 : {
1118 3 : case TYPE_ERROR:
1119 3 : case TYPE_VOID:
1120 3 : case TYPE_BOOLEAN:
1121 3 : case TYPE_INTEGER:
1122 3 : case TYPE_FLOAT:
1123 3 : case TYPE_COMPLEX:
1124 3 : case TYPE_STRING:
1125 3 : case TYPE_NIL:
1126 : // These are simple types that can just be created directly.
1127 3 : return this->get_backend(gogo);
1128 :
1129 29504 : case TYPE_MAP:
1130 29504 : case TYPE_CHANNEL:
1131 : // All maps and channels have the same backend representation.
1132 29504 : return this->get_backend(gogo);
1133 :
1134 2663214 : case TYPE_NAMED:
1135 2663214 : case TYPE_FORWARD:
1136 : // Named types keep track of their own dependencies and manage
1137 : // their own placeholders.
1138 2663214 : if (this->named_type() != NULL && this->named_type()->is_alias())
1139 41241 : return this->unalias()->get_backend_placeholder(gogo);
1140 2621973 : return this->get_backend(gogo);
1141 :
1142 10746 : case TYPE_INTERFACE:
1143 21492 : if (this->interface_type()->is_empty())
1144 10679 : return Interface_type::get_backend_empty_interface_type(gogo);
1145 : break;
1146 :
1147 : default:
1148 : break;
1149 : }
1150 :
1151 1015684 : std::pair<Type*, Type_btype_entry> val;
1152 1015684 : val.first = this;
1153 1015684 : val.second.btype = NULL;
1154 1015684 : val.second.is_placeholder = false;
1155 1015684 : std::pair<Type_btypes::iterator, bool> ins =
1156 1015684 : Type::type_btypes.insert(val);
1157 1015684 : if (!ins.second && ins.first->second.btype != NULL)
1158 : return ins.first->second.btype;
1159 :
1160 435936 : switch (this->classification_)
1161 : {
1162 38889 : case TYPE_FUNCTION:
1163 38889 : {
1164 : // A Go function type is a pointer to a struct type.
1165 77778 : Location loc = this->function_type()->location();
1166 38889 : bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1167 38889 : Type::placeholder_pointers.push_back(this);
1168 : }
1169 38889 : break;
1170 :
1171 212852 : case TYPE_POINTER:
1172 212852 : {
1173 212852 : Location loc = Linemap::unknown_location();
1174 212852 : bt = gogo->backend()->placeholder_pointer_type("", loc, false);
1175 212852 : Type::placeholder_pointers.push_back(this);
1176 : }
1177 212852 : break;
1178 :
1179 19772 : case TYPE_STRUCT:
1180 : // We don't have to make the struct itself be a placeholder. We
1181 : // are promised that we know the sizes of the struct fields.
1182 : // But we may have to use a placeholder for any particular
1183 : // struct field.
1184 19772 : {
1185 19772 : std::vector<Backend::Btyped_identifier> bfields;
1186 39544 : get_backend_struct_fields(gogo, this->struct_type(), true, &bfields);
1187 19772 : bt = gogo->backend()->struct_type(bfields);
1188 19772 : }
1189 19772 : break;
1190 :
1191 164358 : case TYPE_ARRAY:
1192 164358 : if (this->is_slice_type())
1193 : {
1194 81900 : std::vector<Backend::Btyped_identifier> bfields;
1195 163800 : get_backend_slice_fields(gogo, this->array_type(), true, &bfields);
1196 81900 : bt = gogo->backend()->struct_type(bfields);
1197 81900 : }
1198 : else
1199 : {
1200 164916 : Btype* element = this->array_type()->get_backend_element(gogo, true);
1201 164916 : Bexpression* len = this->array_type()->get_backend_length(gogo);
1202 82458 : bt = gogo->backend()->array_type(element, len);
1203 : }
1204 : break;
1205 :
1206 65 : case TYPE_INTERFACE:
1207 65 : {
1208 130 : go_assert(!this->interface_type()->is_empty());
1209 65 : std::vector<Backend::Btyped_identifier> bfields;
1210 130 : get_backend_interface_fields(gogo, this->interface_type(), true,
1211 : &bfields);
1212 65 : bt = gogo->backend()->struct_type(bfields);
1213 65 : }
1214 65 : break;
1215 :
1216 0 : case TYPE_SINK:
1217 0 : case TYPE_CALL_MULTIPLE_RESULT:
1218 : /* Note that various classifications were handled in the earlier
1219 : switch. */
1220 0 : default:
1221 0 : go_unreachable();
1222 : }
1223 :
1224 435936 : if (ins.first->second.btype == NULL)
1225 : {
1226 435936 : ins.first->second.btype = bt;
1227 435936 : ins.first->second.is_placeholder = true;
1228 : }
1229 : else
1230 : {
1231 : // A placeholder for this type got created along the way. Use
1232 : // that one and ignore the one we just built.
1233 : bt = ins.first->second.btype;
1234 : }
1235 :
1236 : return bt;
1237 : }
1238 :
1239 : // Complete the backend representation. This is called for a type
1240 : // using a placeholder type.
1241 :
1242 : void
1243 404536 : Type::finish_backend(Gogo* gogo, Btype *placeholder)
1244 : {
1245 404536 : switch (this->classification_)
1246 : {
1247 0 : case TYPE_ERROR:
1248 0 : case TYPE_VOID:
1249 0 : case TYPE_BOOLEAN:
1250 0 : case TYPE_INTEGER:
1251 0 : case TYPE_FLOAT:
1252 0 : case TYPE_COMPLEX:
1253 0 : case TYPE_STRING:
1254 0 : case TYPE_NIL:
1255 0 : go_unreachable();
1256 :
1257 40419 : case TYPE_FUNCTION:
1258 40419 : {
1259 40419 : Btype* bt = this->do_get_backend(gogo);
1260 40419 : if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1261 0 : go_assert(saw_errors());
1262 : }
1263 : break;
1264 :
1265 247063 : case TYPE_POINTER:
1266 247063 : {
1267 247063 : Btype* bt = this->do_get_backend(gogo);
1268 247063 : if (!gogo->backend()->set_placeholder_pointer_type(placeholder, bt))
1269 0 : go_assert(saw_errors());
1270 : }
1271 : break;
1272 :
1273 10457 : case TYPE_STRUCT:
1274 : // The struct type itself is done, but we have to make sure that
1275 : // all the field types are converted.
1276 10457 : this->struct_type()->finish_backend_fields(gogo);
1277 10457 : break;
1278 :
1279 106535 : case TYPE_ARRAY:
1280 : // The array type itself is done, but make sure the element type
1281 : // is converted.
1282 106535 : this->array_type()->finish_backend_element(gogo);
1283 106535 : break;
1284 :
1285 0 : case TYPE_MAP:
1286 0 : case TYPE_CHANNEL:
1287 0 : go_unreachable();
1288 :
1289 62 : case TYPE_INTERFACE:
1290 : // The interface type itself is done, but make sure the method
1291 : // types are converted.
1292 62 : this->interface_type()->finish_backend_methods(gogo);
1293 62 : break;
1294 :
1295 0 : case TYPE_NAMED:
1296 0 : case TYPE_FORWARD:
1297 0 : go_unreachable();
1298 :
1299 0 : case TYPE_SINK:
1300 0 : case TYPE_CALL_MULTIPLE_RESULT:
1301 0 : default:
1302 0 : go_unreachable();
1303 : }
1304 :
1305 404536 : this->btype_ = placeholder;
1306 404536 : }
1307 :
1308 : // Return a pointer to the type descriptor for this type.
1309 :
1310 : Bexpression*
1311 1563244 : Type::type_descriptor_pointer(Gogo* gogo, Location location)
1312 : {
1313 1563244 : Type* t = this->unalias();
1314 1563244 : if (t->type_descriptor_var_ == NULL)
1315 : {
1316 481785 : t->make_type_descriptor_var(gogo);
1317 481785 : go_assert(t->type_descriptor_var_ != NULL);
1318 : }
1319 1563244 : Bexpression* var_expr =
1320 1563244 : gogo->backend()->var_expression(t->type_descriptor_var_, location);
1321 1563244 : Bexpression* var_addr =
1322 1563244 : gogo->backend()->address_expression(var_expr, location);
1323 1563244 : Type* td_type = Type::make_type_descriptor_type();
1324 1563244 : Btype* td_btype = td_type->get_backend(gogo);
1325 1563244 : Btype* ptd_btype = gogo->backend()->pointer_type(td_btype);
1326 1563244 : return gogo->backend()->convert_expression(ptd_btype, var_addr, location);
1327 : }
1328 :
1329 : // A mapping from unnamed types to type descriptor variables.
1330 :
1331 : Type::Type_descriptor_vars Type::type_descriptor_vars;
1332 :
1333 : // Build the type descriptor for this type.
1334 :
1335 : void
1336 481785 : Type::make_type_descriptor_var(Gogo* gogo)
1337 : {
1338 481785 : go_assert(this->type_descriptor_var_ == NULL);
1339 :
1340 481785 : Named_type* nt = this->named_type();
1341 :
1342 : // We can have multiple instances of unnamed types, but we only want
1343 : // to emit the type descriptor once. We use a hash table. This is
1344 : // not necessary for named types, as they are unique, and we store
1345 : // the type descriptor in the type itself.
1346 481785 : Bvariable** phash = NULL;
1347 481785 : if (nt == NULL)
1348 : {
1349 418746 : Bvariable* bvnull = NULL;
1350 418746 : std::pair<Type_descriptor_vars::iterator, bool> ins =
1351 418746 : Type::type_descriptor_vars.insert(std::make_pair(this, bvnull));
1352 418746 : if (!ins.second)
1353 : {
1354 : // We've already built a type descriptor for this type.
1355 196999 : this->type_descriptor_var_ = ins.first->second;
1356 196999 : return;
1357 : }
1358 221747 : phash = &ins.first->second;
1359 : }
1360 :
1361 : // The type descriptor symbol for the unsafe.Pointer type is defined in
1362 : // libgo/go-unsafe-pointer.c, so we just return a reference to that
1363 : // symbol if necessary.
1364 284786 : if (this->is_unsafe_pointer_type())
1365 : {
1366 1178 : Location bloc = Linemap::predeclared_location();
1367 :
1368 1178 : Type* td_type = Type::make_type_descriptor_type();
1369 1178 : Btype* td_btype = td_type->get_backend(gogo);
1370 1178 : Backend_name bname;
1371 1178 : gogo->type_descriptor_backend_name(this, nt, &bname);
1372 2356 : this->type_descriptor_var_ =
1373 2356 : gogo->backend()->immutable_struct_reference(bname.name(),
1374 1178 : bname.optional_asm_name(),
1375 : td_btype,
1376 : bloc);
1377 :
1378 1178 : if (phash != NULL)
1379 1095 : *phash = this->type_descriptor_var_;
1380 1178 : return;
1381 1178 : }
1382 :
1383 283608 : Backend_name bname;
1384 283608 : gogo->type_descriptor_backend_name(this, nt, &bname);
1385 :
1386 : // Build the contents of the type descriptor.
1387 283608 : Expression* initializer = this->do_type_descriptor(gogo, NULL);
1388 283608 : initializer->determine_type_no_context(gogo);
1389 :
1390 283608 : Btype* initializer_btype = initializer->type()->get_backend(gogo);
1391 :
1392 283608 : Location loc = nt == NULL ? Linemap::predeclared_location() : nt->location();
1393 :
1394 283608 : const Package* dummy;
1395 283608 : if (this->type_descriptor_defined_elsewhere(nt, &dummy))
1396 : {
1397 75298 : this->type_descriptor_var_ =
1398 75298 : gogo->backend()->immutable_struct_reference(bname.name(),
1399 37649 : bname.optional_asm_name(),
1400 : initializer_btype,
1401 : loc);
1402 37649 : if (phash != NULL)
1403 17806 : *phash = this->type_descriptor_var_;
1404 37649 : return;
1405 : }
1406 :
1407 : // See if this type descriptor can appear in multiple packages.
1408 245959 : bool is_common = false;
1409 245959 : if (nt != NULL)
1410 : {
1411 : // We create the descriptor for a builtin type whenever we need
1412 : // it.
1413 43113 : is_common = nt->is_builtin();
1414 : }
1415 : else
1416 : {
1417 : // This is an unnamed type. The descriptor could be defined in
1418 : // any package where it is needed, and the linker will pick one
1419 : // descriptor to keep.
1420 : is_common = true;
1421 : }
1422 :
1423 : // We are going to build the type descriptor in this package. We
1424 : // must create the variable before we convert the initializer to the
1425 : // backend representation, because the initializer may refer to the
1426 : // type descriptor of this type. By setting type_descriptor_var_ we
1427 : // ensure that type_descriptor_pointer will work if called while
1428 : // converting INITIALIZER.
1429 :
1430 43113 : unsigned int flags = 0;
1431 43113 : if (is_common)
1432 : flags |= Backend::variable_is_common;
1433 491918 : this->type_descriptor_var_ =
1434 491918 : gogo->backend()->immutable_struct(bname.name(), bname.optional_asm_name(),
1435 : flags, initializer_btype, loc);
1436 245959 : if (phash != NULL)
1437 202846 : *phash = this->type_descriptor_var_;
1438 :
1439 245959 : Translate_context context(gogo, NULL, NULL, NULL);
1440 245959 : context.set_is_const();
1441 245959 : Bexpression* binitializer = initializer->get_backend(&context);
1442 :
1443 245959 : gogo->backend()->immutable_struct_set_init(this->type_descriptor_var_,
1444 245959 : bname.name(), flags,
1445 : initializer_btype, loc,
1446 : binitializer);
1447 :
1448 : // For types that may be created by reflection, add it to the
1449 : // list of which we will register the type descriptor to the
1450 : // runtime.
1451 : // Do not add generated incomparable array/struct types, see
1452 : // issue #22605.
1453 245959 : if (is_common
1454 245959 : && (this->points_to() != NULL
1455 154678 : || this->channel_type() != NULL
1456 327109 : || this->map_type() != NULL
1457 176263 : || this->function_type() != NULL
1458 57820 : || this->is_slice_type()
1459 54174 : || (this->struct_type() != NULL
1460 11938 : && !this->struct_type()->is_struct_incomparable())
1461 18648 : || (this->array_type() != NULL
1462 18648 : && !this->array_type()->is_array_incomparable())))
1463 176263 : gogo->add_type_descriptor(this);
1464 283608 : }
1465 :
1466 : // Return true if this type descriptor is defined in a different
1467 : // package. If this returns true it sets *PACKAGE to the package.
1468 :
1469 : bool
1470 603369 : Type::type_descriptor_defined_elsewhere(Named_type* nt,
1471 : const Package** package)
1472 : {
1473 603369 : if (nt != NULL)
1474 : {
1475 303009 : if (nt->named_object()->package() != NULL)
1476 : {
1477 : // This is a named type defined in a different package. The
1478 : // type descriptor should be defined in that package.
1479 246909 : *package = nt->named_object()->package();
1480 246909 : return true;
1481 : }
1482 : }
1483 : else
1484 : {
1485 300360 : if (this->points_to() != NULL
1486 77110 : && this->points_to()->unalias()->named_type() != NULL
1487 361277 : && this->points_to()->unalias()->named_type()->named_object()->package() != NULL)
1488 : {
1489 : // This is an unnamed pointer to a named type defined in a
1490 : // different package. The descriptor should be defined in
1491 : // that package.
1492 17806 : *package = this->points_to()->unalias()->named_type()->named_object()->package();
1493 17806 : return true;
1494 : }
1495 : }
1496 : return false;
1497 : }
1498 :
1499 : // Return a composite literal for a type descriptor.
1500 :
1501 : Expression*
1502 0 : Type::type_descriptor(Gogo* gogo, Type* type)
1503 : {
1504 0 : Expression* ret = type->do_type_descriptor(gogo, NULL);
1505 0 : ret->determine_type_no_context(gogo);
1506 0 : return ret;
1507 : }
1508 :
1509 : // Return a composite literal for a type descriptor with a name.
1510 :
1511 : Expression*
1512 77446 : Type::named_type_descriptor(Gogo* gogo, Type* type, Named_type* name)
1513 : {
1514 77446 : go_assert(name != NULL && type->named_type() != name);
1515 77446 : Expression* ret = type->do_type_descriptor(gogo, name);
1516 77446 : ret->determine_type_no_context(gogo);
1517 77446 : return ret;
1518 : }
1519 :
1520 : // Make a builtin struct type from a list of fields. The fields are
1521 : // pairs of a name and a type.
1522 :
1523 : Struct_type*
1524 100108 : Type::make_builtin_struct_type(int nfields, ...)
1525 : {
1526 100108 : va_list ap;
1527 100108 : va_start(ap, nfields);
1528 :
1529 100108 : Location bloc = Linemap::predeclared_location();
1530 100108 : Struct_field_list* sfl = new Struct_field_list();
1531 560802 : for (int i = 0; i < nfields; i++)
1532 : {
1533 460694 : const char* field_name = va_arg(ap, const char *);
1534 460694 : Type* type = va_arg(ap, Type*);
1535 921388 : sfl->push_back(Struct_field(Typed_identifier(field_name, type, bloc)));
1536 : }
1537 :
1538 100108 : va_end(ap);
1539 :
1540 100108 : Struct_type* ret = Type::make_struct_type(sfl, bloc);
1541 100108 : ret->set_is_struct_incomparable();
1542 100108 : return ret;
1543 : }
1544 :
1545 : // A list of builtin named types.
1546 :
1547 : std::vector<Named_type*> Type::named_builtin_types;
1548 :
1549 : // Make a builtin named type.
1550 :
1551 : Named_type*
1552 65044 : Type::make_builtin_named_type(const char* name, Type* type)
1553 : {
1554 65044 : Location bloc = Linemap::predeclared_location();
1555 65044 : Named_object* no = Named_object::make_type(name, NULL, type, bloc);
1556 65044 : Named_type* ret = no->type_value();
1557 65044 : Type::named_builtin_types.push_back(ret);
1558 65044 : return ret;
1559 : }
1560 :
1561 : // Convert the named builtin types.
1562 :
1563 : void
1564 4646 : Type::convert_builtin_named_types(Gogo* gogo)
1565 : {
1566 69690 : for (std::vector<Named_type*>::const_iterator p =
1567 4646 : Type::named_builtin_types.begin();
1568 69690 : p != Type::named_builtin_types.end();
1569 65044 : ++p)
1570 : {
1571 65044 : bool r = (*p)->verify(gogo);
1572 65044 : go_assert(r);
1573 65044 : (*p)->convert(gogo);
1574 : }
1575 4646 : }
1576 :
1577 : // Values to store in the tflag field of a type descriptor. This must
1578 : // match the definitions in libgo/go/runtime/type.go.
1579 :
1580 : const int TFLAG_REGULAR_MEMORY = 1 << 3;
1581 :
1582 : // Return the type of a type descriptor. We should really tie this to
1583 : // runtime.Type rather than copying it. This must match the struct "_type"
1584 : // declared in libgo/go/runtime/type.go.
1585 :
1586 : Type*
1587 1894485 : Type::make_type_descriptor_type()
1588 : {
1589 1894485 : static Type* ret;
1590 1894485 : if (ret == NULL)
1591 : {
1592 4646 : Location bloc = Linemap::predeclared_location();
1593 :
1594 4646 : Type* uint8_type = Type::lookup_integer_type("uint8");
1595 4646 : Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
1596 4646 : Type* uint32_type = Type::lookup_integer_type("uint32");
1597 4646 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
1598 4646 : Type* string_type = Type::lookup_string_type();
1599 4646 : Type* pointer_string_type = Type::make_pointer_type(string_type);
1600 :
1601 : // This is an unnamed version of unsafe.Pointer. Perhaps we
1602 : // should use the named version instead, although that would
1603 : // require us to create the unsafe package if it has not been
1604 : // imported. It probably doesn't matter.
1605 4646 : Type* void_type = Type::make_void_type();
1606 4646 : Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1607 :
1608 4646 : Typed_identifier_list* params = new Typed_identifier_list();
1609 9292 : params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
1610 9292 : params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
1611 :
1612 4646 : Typed_identifier_list* results = new Typed_identifier_list();
1613 9292 : results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
1614 :
1615 4646 : Type* equal_fntype = Type::make_function_type(NULL, params, results,
1616 : bloc);
1617 :
1618 : // Forward declaration for the type descriptor type.
1619 4646 : Named_object* named_type_descriptor_type =
1620 4646 : Named_object::make_type_declaration("_type", NULL, bloc);
1621 4646 : Type* ft = Type::make_forward_declaration(named_type_descriptor_type);
1622 4646 : Type* pointer_type_descriptor_type = Type::make_pointer_type(ft);
1623 :
1624 : // The type of a method on a concrete type.
1625 4646 : Struct_type* method_type =
1626 4646 : Type::make_builtin_struct_type(5,
1627 : "name", pointer_string_type,
1628 : "pkgPath", pointer_string_type,
1629 : "mtyp", pointer_type_descriptor_type,
1630 : "typ", pointer_type_descriptor_type,
1631 : "tfn", unsafe_pointer_type);
1632 4646 : Named_type* named_method_type =
1633 4646 : Type::make_builtin_named_type("method", method_type);
1634 :
1635 : // Information for types with a name or methods.
1636 4646 : Type* slice_named_method_type =
1637 4646 : Type::make_array_type(named_method_type, NULL);
1638 4646 : Struct_type* uncommon_type =
1639 4646 : Type::make_builtin_struct_type(3,
1640 : "name", pointer_string_type,
1641 : "pkgPath", pointer_string_type,
1642 : "methods", slice_named_method_type);
1643 4646 : Named_type* named_uncommon_type =
1644 4646 : Type::make_builtin_named_type("uncommonType", uncommon_type);
1645 :
1646 4646 : Type* pointer_uncommon_type =
1647 4646 : Type::make_pointer_type(named_uncommon_type);
1648 :
1649 : // The type descriptor type.
1650 :
1651 4646 : Struct_type* type_descriptor_type =
1652 4646 : Type::make_builtin_struct_type(12,
1653 : "size", uintptr_type,
1654 : "ptrdata", uintptr_type,
1655 : "hash", uint32_type,
1656 : "tflag", uint8_type,
1657 : "align", uint8_type,
1658 : "fieldAlign", uint8_type,
1659 : "kind", uint8_type,
1660 : "equal", equal_fntype,
1661 : "gcdata", pointer_uint8_type,
1662 : "string", pointer_string_type,
1663 : "", pointer_uncommon_type,
1664 : "ptrToThis",
1665 : pointer_type_descriptor_type);
1666 :
1667 4646 : Named_type* named = Type::make_builtin_named_type("_type",
1668 : type_descriptor_type);
1669 :
1670 4646 : named_type_descriptor_type->set_type_value(named);
1671 :
1672 4646 : ret = named;
1673 : }
1674 :
1675 1894485 : return ret;
1676 : }
1677 :
1678 : // Make the type of a pointer to a type descriptor as represented in
1679 : // Go.
1680 :
1681 : Type*
1682 2767362 : Type::make_type_descriptor_ptr_type()
1683 : {
1684 2767362 : static Type* ret;
1685 2767362 : if (ret == NULL)
1686 4646 : ret = Type::make_pointer_type(Type::make_type_descriptor_type());
1687 2767362 : return ret;
1688 : }
1689 :
1690 : // Return the alignment required by the memequalN function. N is a
1691 : // type size: 16, 32, 64, or 128. The memequalN functions are defined
1692 : // in libgo/go/runtime/alg.go.
1693 :
1694 : int64_t
1695 406420 : Type::memequal_align(Gogo* gogo, int size)
1696 : {
1697 406420 : const char* tn;
1698 406420 : switch (size)
1699 : {
1700 : case 16:
1701 : tn = "int16";
1702 : break;
1703 111605 : case 32:
1704 111605 : tn = "int32";
1705 111605 : break;
1706 : case 64:
1707 243356 : tn = "int64";
1708 : break;
1709 : case 128:
1710 : // The code uses [2]int64, which must have the same alignment as
1711 : // int64.
1712 243356 : tn = "int64";
1713 : break;
1714 0 : default:
1715 0 : go_unreachable();
1716 : }
1717 :
1718 406420 : Type* t = Type::lookup_integer_type(tn);
1719 :
1720 406420 : int64_t ret;
1721 406420 : if (!t->backend_type_align(gogo, &ret))
1722 0 : go_unreachable();
1723 406420 : return ret;
1724 : }
1725 :
1726 : // Return whether this type needs specially built type functions.
1727 : // This returns true for types that are comparable and either can not
1728 : // use an identity comparison, or are a non-standard size.
1729 :
1730 : bool
1731 1423302 : Type::needs_specific_type_functions(Gogo* gogo)
1732 : {
1733 1423302 : Named_type* nt = this->named_type();
1734 1423302 : if (nt != NULL && nt->is_alias())
1735 : return false;
1736 1423302 : if (!this->is_comparable())
1737 : return false;
1738 785813 : if (!this->compare_is_identity(gogo))
1739 : return true;
1740 :
1741 : // We create a few predeclared types for type descriptors; they are
1742 : // really just for the backend and don't need hash or equality
1743 : // functions.
1744 473127 : if (nt != NULL && Linemap::is_predeclared_location(nt->location()))
1745 : return false;
1746 :
1747 428436 : int64_t size, align;
1748 428436 : if (!this->backend_type_size(gogo, &size)
1749 428436 : || !this->backend_type_align(gogo, &align))
1750 : {
1751 0 : go_assert(saw_errors());
1752 : return false;
1753 : }
1754 : // This switch matches the one in Type::equal_function.
1755 428436 : switch (size)
1756 : {
1757 49936 : case 0:
1758 49936 : case 1:
1759 49936 : case 2:
1760 49936 : return align < Type::memequal_align(gogo, 16);
1761 68109 : case 4:
1762 68109 : return align < Type::memequal_align(gogo, 32);
1763 114603 : case 8:
1764 114603 : return align < Type::memequal_align(gogo, 64);
1765 35388 : case 16:
1766 35388 : return align < Type::memequal_align(gogo, 128);
1767 : default:
1768 : return true;
1769 : }
1770 : }
1771 :
1772 : // Return the runtime function that computes the hash of this type.
1773 : // HASH_FNTYPE is the type of the hash function function, for
1774 : // convenience; it may be NULL. This returns NULL if the type is not
1775 : // comparable.
1776 :
1777 : Named_object*
1778 42438 : Type::hash_function(Gogo* gogo, Function_type* hash_fntype)
1779 : {
1780 42438 : if (this->named_type() != NULL)
1781 40412 : go_assert(!this->named_type()->is_alias());
1782 :
1783 42438 : if (!this->is_comparable())
1784 : return NULL;
1785 :
1786 42438 : if (hash_fntype == NULL)
1787 : {
1788 37920 : Location bloc = Linemap::predeclared_location();
1789 37920 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
1790 37920 : Type* void_type = Type::make_void_type();
1791 37920 : Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
1792 37920 : Typed_identifier_list* params = new Typed_identifier_list();
1793 75840 : params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
1794 75840 : params->push_back(Typed_identifier("seed", uintptr_type, bloc));
1795 37920 : Typed_identifier_list* results = new Typed_identifier_list();
1796 75840 : results->push_back(Typed_identifier("", uintptr_type, bloc));
1797 37920 : hash_fntype = Type::make_function_type(NULL, params, results, bloc);
1798 : }
1799 :
1800 42438 : const char* hash_fnname;
1801 42438 : if (this->compare_is_identity(gogo))
1802 : {
1803 2331 : int64_t size;
1804 2331 : if (!this->backend_type_size(gogo, &size))
1805 : {
1806 0 : go_assert(saw_errors());
1807 343 : return NULL;
1808 : }
1809 2331 : switch (size)
1810 : {
1811 : case 0:
1812 : hash_fnname = "runtime.memhash0";
1813 : break;
1814 376 : case 1:
1815 376 : hash_fnname = "runtime.memhash8";
1816 376 : break;
1817 67 : case 2:
1818 67 : hash_fnname = "runtime.memhash16";
1819 67 : break;
1820 560 : case 4:
1821 560 : hash_fnname = "runtime.memhash32";
1822 560 : break;
1823 949 : case 8:
1824 949 : hash_fnname = "runtime.memhash64";
1825 949 : break;
1826 7 : case 16:
1827 7 : hash_fnname = "runtime.memhash128";
1828 7 : break;
1829 343 : default:
1830 : // We don't have a built-in function for a type of this
1831 : // size. Build a function to use that calls the generic
1832 : // hash functions for identity, passing the size.
1833 343 : return this->build_hash_function(gogo, size, hash_fntype);
1834 : }
1835 : }
1836 : else
1837 : {
1838 40107 : switch (this->base()->classification())
1839 : {
1840 0 : case Type::TYPE_ERROR:
1841 0 : case Type::TYPE_VOID:
1842 0 : case Type::TYPE_NIL:
1843 0 : case Type::TYPE_FUNCTION:
1844 0 : case Type::TYPE_MAP:
1845 : // For these types is_comparable should have returned false.
1846 0 : go_unreachable();
1847 :
1848 0 : case Type::TYPE_BOOLEAN:
1849 0 : case Type::TYPE_INTEGER:
1850 0 : case Type::TYPE_POINTER:
1851 0 : case Type::TYPE_CHANNEL:
1852 : // For these types compare_is_identity should have returned true.
1853 0 : go_unreachable();
1854 :
1855 251 : case Type::TYPE_FLOAT:
1856 502 : switch (this->float_type()->bits())
1857 : {
1858 : case 32:
1859 : hash_fnname = "runtime.f32hash";
1860 : break;
1861 227 : case 64:
1862 227 : hash_fnname = "runtime.f64hash";
1863 227 : break;
1864 0 : default:
1865 0 : go_unreachable();
1866 : }
1867 : break;
1868 :
1869 34 : case Type::TYPE_COMPLEX:
1870 68 : switch (this->complex_type()->bits())
1871 : {
1872 : case 64:
1873 : hash_fnname = "runtime.c64hash";
1874 : break;
1875 24 : case 128:
1876 24 : hash_fnname = "runtime.c128hash";
1877 24 : break;
1878 0 : default:
1879 0 : go_unreachable();
1880 : }
1881 : break;
1882 :
1883 : case Type::TYPE_STRING:
1884 : hash_fnname = "runtime.strhash";
1885 : break;
1886 :
1887 2541 : case Type::TYPE_STRUCT:
1888 : // This is a struct which can not be compared using a simple
1889 : // identity function. We need to build a function to
1890 : // compute the hash.
1891 2541 : return this->build_hash_function(gogo, -1, hash_fntype);
1892 :
1893 135 : case Type::TYPE_ARRAY:
1894 135 : if (this->is_slice_type())
1895 : {
1896 : // Type::is_compatible_for_comparison should have
1897 : // returned false.
1898 0 : go_unreachable();
1899 : }
1900 : else
1901 : {
1902 : // This is an array which can not be compared using a
1903 : // simple identity function. We need to build a
1904 : // function to compute the hash.
1905 135 : return this->build_hash_function(gogo, -1, hash_fntype);
1906 : }
1907 4336 : break;
1908 :
1909 4336 : case Type::TYPE_INTERFACE:
1910 8672 : if (this->interface_type()->is_empty())
1911 : hash_fnname = "runtime.nilinterhash";
1912 : else
1913 3192 : hash_fnname = "runtime.interhash";
1914 : break;
1915 :
1916 0 : case Type::TYPE_NAMED:
1917 0 : case Type::TYPE_FORWARD:
1918 0 : go_unreachable();
1919 :
1920 0 : default:
1921 0 : go_unreachable();
1922 : }
1923 : }
1924 :
1925 39419 : Location bloc = Linemap::predeclared_location();
1926 39419 : Named_object *hash_fn = Named_object::make_function_declaration(hash_fnname,
1927 : NULL,
1928 : hash_fntype,
1929 : bloc);
1930 39419 : hash_fn->func_declaration_value()->set_asm_name(hash_fnname);
1931 39419 : return hash_fn;
1932 : }
1933 :
1934 : // A hash table mapping types to the specific hash functions.
1935 :
1936 : Type::Type_function Type::type_hash_functions_table;
1937 :
1938 : // Build a hash function that is specific to a type: if SIZE == -1,
1939 : // this is a struct or array type that cannot use an identity
1940 : // comparison. Otherwise, it is a type that uses an identity
1941 : // comparison but is not one of the standard supported sizes.
1942 : //
1943 : // Unlike an equality function, hash functions are not in type
1944 : // descriptors, so we can't assume that a named type has defined a
1945 : // hash function in the package that defines the type. So hash
1946 : // functions are always defined locally. FIXME: It would be better to
1947 : // define hash functions with comdat linkage so that duplicate hash
1948 : // functions can be coalesced at link time.
1949 :
1950 : Named_object*
1951 3019 : Type::build_hash_function(Gogo* gogo, int64_t size, Function_type* hash_fntype)
1952 : {
1953 3019 : Type* type = this->base();
1954 :
1955 3019 : std::pair<Type*, Named_object*> val(type, NULL);
1956 3019 : std::pair<Type_function::iterator, bool> ins =
1957 3019 : Type::type_hash_functions_table.insert(val);
1958 3019 : if (!ins.second)
1959 : {
1960 : // We already have a function for this type.
1961 2790 : return ins.first->second;
1962 : }
1963 :
1964 229 : Backend_name bname;
1965 229 : gogo->hash_function_name(type, &bname);
1966 :
1967 229 : Location bloc = Linemap::predeclared_location();
1968 :
1969 229 : Named_object* hash_fn = gogo->declare_package_function(bname.name(),
1970 : hash_fntype, bloc);
1971 :
1972 229 : ins.first->second = hash_fn;
1973 :
1974 229 : if (gogo->in_global_scope())
1975 228 : type->write_hash_function(gogo, size, &bname, hash_fntype);
1976 : else
1977 1 : gogo->queue_hash_function(type, size, &bname, hash_fntype);
1978 :
1979 229 : return hash_fn;
1980 229 : }
1981 :
1982 : // Write the hash function for a type that needs it written specially.
1983 :
1984 : void
1985 229 : Type::write_hash_function(Gogo* gogo, int64_t size, const Backend_name* bname,
1986 : Function_type* hash_fntype)
1987 : {
1988 229 : Location bloc = Linemap::predeclared_location();
1989 :
1990 229 : if (gogo->specific_type_functions_are_written())
1991 : {
1992 0 : go_assert(saw_errors());
1993 0 : return;
1994 : }
1995 :
1996 229 : go_assert(this->is_comparable());
1997 :
1998 229 : Named_object* hash_fn = gogo->start_function(bname->name(), hash_fntype,
1999 : false, bloc);
2000 229 : hash_fn->func_value()->set_asm_name(bname->asm_name());
2001 229 : hash_fn->func_value()->set_is_type_specific_function();
2002 229 : gogo->start_block(bloc);
2003 :
2004 229 : if (size != -1)
2005 48 : this->write_identity_hash(gogo, hash_fn, size);
2006 181 : else if (this->struct_type() != NULL)
2007 308 : this->struct_type()->write_hash_function(gogo, hash_fn, hash_fntype);
2008 27 : else if (this->array_type() != NULL)
2009 54 : this->array_type()->write_hash_function(gogo, hash_fn, hash_fntype);
2010 : else
2011 0 : go_unreachable();
2012 :
2013 229 : Block* b = gogo->finish_block(bloc);
2014 229 : gogo->add_block(b, bloc);
2015 229 : b->determine_types(gogo);
2016 229 : gogo->lower_block(hash_fn, b);
2017 229 : gogo->order_block(b);
2018 229 : gogo->remove_shortcuts_in_block(b);
2019 229 : gogo->finish_function(bloc);
2020 :
2021 : // Build the function descriptor for the type descriptor to refer to.
2022 229 : hash_fn->func_value()->descriptor(gogo, hash_fn);
2023 : }
2024 :
2025 : // Write a hash function for a type that can use an identity hash but
2026 : // is not one of the standard supported sizes. For example, this
2027 : // would be used for the type [3]byte. This builds a return statement
2028 : // that returns a call to the memhash function, passing the key and
2029 : // seed from the function arguments (already constructed before this
2030 : // is called), and the constant size.
2031 :
2032 : void
2033 48 : Type::write_identity_hash(Gogo* gogo, Named_object* function, int64_t size)
2034 : {
2035 48 : Location bloc = Linemap::predeclared_location();
2036 :
2037 48 : Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2038 48 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
2039 :
2040 48 : Typed_identifier_list* params = new Typed_identifier_list();
2041 96 : params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
2042 96 : params->push_back(Typed_identifier("seed", uintptr_type, bloc));
2043 96 : params->push_back(Typed_identifier("size", uintptr_type, bloc));
2044 :
2045 48 : Typed_identifier_list* results = new Typed_identifier_list();
2046 96 : results->push_back(Typed_identifier("", uintptr_type, bloc));
2047 :
2048 48 : Function_type* memhash_fntype = Type::make_function_type(NULL, params,
2049 : results, bloc);
2050 :
2051 48 : Named_object* memhash =
2052 48 : Named_object::make_function_declaration("runtime.memhash", NULL,
2053 : memhash_fntype, bloc);
2054 48 : memhash->func_declaration_value()->set_asm_name("runtime.memhash");
2055 :
2056 48 : Named_object* key_arg = gogo->lookup("key", NULL);
2057 48 : go_assert(key_arg != NULL);
2058 48 : Named_object* seed_arg = gogo->lookup("seed", NULL);
2059 48 : go_assert(seed_arg != NULL);
2060 :
2061 48 : Expression* key_ref = Expression::make_var_reference(key_arg, bloc);
2062 48 : Expression* seed_ref = Expression::make_var_reference(seed_arg, bloc);
2063 48 : Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2064 : bloc);
2065 48 : Expression_list* args = new Expression_list();
2066 48 : args->push_back(key_ref);
2067 48 : args->push_back(seed_ref);
2068 48 : args->push_back(size_arg);
2069 48 : Expression* func = Expression::make_func_reference(memhash, NULL, bloc);
2070 48 : Expression* call = Expression::make_call(func, args, false, bloc);
2071 :
2072 48 : Expression_list* vals = new Expression_list();
2073 48 : vals->push_back(call);
2074 48 : Statement* s = Statement::make_return_statement(function, vals, bloc);
2075 48 : s->determine_types(gogo);
2076 48 : gogo->add_statement(s);
2077 48 : }
2078 :
2079 : // Return the runtime function that compares whether two values of
2080 : // this type are equal. If NAME is not NULL it is the name of this
2081 : // type. EQUAL_FNTYPE is the type of the equality function, for
2082 : // convenience; it may be NULL. This returns NULL if the type is not
2083 : // comparable.
2084 :
2085 : Named_object*
2086 810422 : Type::equal_function(Gogo* gogo, Named_type* name, Function_type* equal_fntype)
2087 : {
2088 810422 : if (this->named_type() != NULL)
2089 360328 : go_assert(!this->named_type()->is_alias());
2090 :
2091 : // If the unaliased type is not a named type, then the type does not
2092 : // have a name after all.
2093 810422 : if (name != NULL)
2094 423283 : name = name->unalias()->named_type();
2095 :
2096 810422 : if (!this->is_comparable())
2097 : return NULL;
2098 :
2099 661178 : if (equal_fntype == NULL)
2100 : {
2101 526819 : Location bloc = Linemap::predeclared_location();
2102 526819 : Type* void_type = Type::make_void_type();
2103 526819 : Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
2104 526819 : Typed_identifier_list* params = new Typed_identifier_list();
2105 1053638 : params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
2106 1053638 : params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
2107 526819 : Typed_identifier_list* results = new Typed_identifier_list();
2108 1053638 : results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
2109 526819 : equal_fntype = Type::make_function_type(NULL, params, results, bloc);
2110 : }
2111 :
2112 661178 : const char* equal_fnname;
2113 661178 : if (this->compare_is_identity(gogo))
2114 : {
2115 362615 : int64_t size, align;
2116 362615 : if (!this->backend_type_size(gogo, &size)
2117 362615 : || !this->backend_type_align(gogo, &align))
2118 : {
2119 0 : go_assert(saw_errors());
2120 217600 : return NULL;
2121 : }
2122 362615 : bool build_function = false;
2123 : // This switch matches the one in Type::needs_specific_type_functions.
2124 : // The alignment tests are because of the memequal functions,
2125 : // which assume that the values are aligned as required for an
2126 : // integer of that size.
2127 362615 : switch (size)
2128 : {
2129 : case 0:
2130 : equal_fnname = "runtime.memequal0";
2131 : break;
2132 : case 1:
2133 : equal_fnname = "runtime.memequal8";
2134 : break;
2135 1523 : case 2:
2136 1523 : if (align < Type::memequal_align(gogo, 16))
2137 : build_function = true;
2138 : else
2139 : equal_fnname = "runtime.memequal16";
2140 : break;
2141 43496 : case 4:
2142 43496 : if (align < Type::memequal_align(gogo, 32))
2143 : build_function = true;
2144 : else
2145 : equal_fnname = "runtime.memequal32";
2146 : break;
2147 81457 : case 8:
2148 81457 : if (align < Type::memequal_align(gogo, 64))
2149 : build_function = true;
2150 : else
2151 : equal_fnname = "runtime.memequal64";
2152 : break;
2153 11908 : case 16:
2154 11908 : if (align < Type::memequal_align(gogo, 128))
2155 : build_function = true;
2156 : else
2157 : equal_fnname = "runtime.memequal128";
2158 : break;
2159 : default:
2160 : build_function = true;
2161 : break;
2162 : }
2163 : if (build_function)
2164 : {
2165 : // We don't have a built-in function for a type of this size
2166 : // and alignment. Build a function to use that calls the
2167 : // generic equality functions for identity, passing the size.
2168 217600 : return this->build_equal_function(gogo, name, size, equal_fntype);
2169 : }
2170 : }
2171 : else
2172 : {
2173 298563 : switch (this->base()->classification())
2174 : {
2175 0 : case Type::TYPE_ERROR:
2176 0 : case Type::TYPE_VOID:
2177 0 : case Type::TYPE_NIL:
2178 0 : case Type::TYPE_FUNCTION:
2179 0 : case Type::TYPE_MAP:
2180 : // For these types is_comparable should have returned false.
2181 0 : go_unreachable();
2182 :
2183 0 : case Type::TYPE_BOOLEAN:
2184 0 : case Type::TYPE_INTEGER:
2185 0 : case Type::TYPE_POINTER:
2186 0 : case Type::TYPE_CHANNEL:
2187 : // For these types compare_is_identity should have returned true.
2188 0 : go_unreachable();
2189 :
2190 4357 : case Type::TYPE_FLOAT:
2191 8714 : switch (this->float_type()->bits())
2192 : {
2193 : case 32:
2194 : equal_fnname = "runtime.f32equal";
2195 : break;
2196 3810 : case 64:
2197 3810 : equal_fnname = "runtime.f64equal";
2198 3810 : break;
2199 0 : default:
2200 0 : go_unreachable();
2201 : }
2202 : break;
2203 :
2204 935 : case Type::TYPE_COMPLEX:
2205 1870 : switch (this->complex_type()->bits())
2206 : {
2207 : case 64:
2208 : equal_fnname = "runtime.c64equal";
2209 : break;
2210 801 : case 128:
2211 801 : equal_fnname = "runtime.c128equal";
2212 801 : break;
2213 0 : default:
2214 0 : go_unreachable();
2215 : }
2216 : break;
2217 :
2218 : case Type::TYPE_STRING:
2219 : equal_fnname = "runtime.strequal";
2220 : break;
2221 :
2222 193354 : case Type::TYPE_STRUCT:
2223 : // This is a struct which can not be compared using a simple
2224 : // identity function. We need to build a function for
2225 : // comparison.
2226 193354 : return this->build_equal_function(gogo, name, -1, equal_fntype);
2227 :
2228 10744 : case Type::TYPE_ARRAY:
2229 10744 : if (this->is_slice_type())
2230 : {
2231 : // Type::is_compatible_for_comparison should have
2232 : // returned false.
2233 0 : go_unreachable();
2234 : }
2235 : else
2236 : {
2237 : // This is an array which can not be compared using a
2238 : // simple identity function. We need to build a
2239 : // function for comparison.
2240 10744 : return this->build_equal_function(gogo, name, -1, equal_fntype);
2241 : }
2242 79232 : break;
2243 :
2244 79232 : case Type::TYPE_INTERFACE:
2245 158464 : if (this->interface_type()->is_empty())
2246 : equal_fnname = "runtime.nilinterequal";
2247 : else
2248 77375 : equal_fnname = "runtime.interequal";
2249 : break;
2250 :
2251 0 : case Type::TYPE_NAMED:
2252 0 : case Type::TYPE_FORWARD:
2253 0 : go_unreachable();
2254 :
2255 0 : default:
2256 0 : go_unreachable();
2257 : }
2258 : }
2259 :
2260 239480 : Location bloc = Linemap::predeclared_location();
2261 239480 : Named_object* equal_fn =
2262 239480 : Named_object::make_function_declaration(equal_fnname, NULL, equal_fntype,
2263 : bloc);
2264 239480 : equal_fn->func_declaration_value()->set_asm_name(equal_fnname);
2265 239480 : return equal_fn;
2266 : }
2267 :
2268 : // A hash table mapping types to the specific equal functions.
2269 :
2270 : Type::Type_function Type::type_equal_functions_table;
2271 :
2272 : // Build an equality function that is specific to a type: if SIZE ==
2273 : // -1, this is a struct or array type that cannot use an identity
2274 : // comparison. Otherwise, it is a type that uses an identity
2275 : // comparison but is not one of the standard supported sizes or it is
2276 : // not aligned as needed.
2277 :
2278 : Named_object*
2279 421698 : Type::build_equal_function(Gogo* gogo, Named_type* name, int64_t size,
2280 : Function_type* equal_fntype)
2281 : {
2282 582212 : std::pair<Type*, Named_object*> val(name != NULL ? name : this, nullptr);
2283 421698 : std::pair<Type_function::iterator, bool> ins =
2284 421698 : Type::type_equal_functions_table.insert(val);
2285 421698 : if (!ins.second)
2286 : {
2287 : // We already have a function for this type.
2288 102051 : return ins.first->second;
2289 : }
2290 :
2291 319647 : Backend_name bname;
2292 319647 : gogo->equal_function_name(this, name, &bname);
2293 :
2294 319647 : Location bloc = Linemap::predeclared_location();
2295 :
2296 319647 : const Package* package = NULL;
2297 319647 : bool is_defined_elsewhere =
2298 319647 : this->type_descriptor_defined_elsewhere(name, &package);
2299 :
2300 319647 : Named_object* equal_fn;
2301 319647 : if (is_defined_elsewhere)
2302 227066 : equal_fn = Named_object::make_function_declaration(bname.name(), package,
2303 : equal_fntype, bloc);
2304 : else
2305 92581 : equal_fn = gogo->declare_package_function(bname.name(), equal_fntype, bloc);
2306 :
2307 319647 : ins.first->second = equal_fn;
2308 :
2309 319647 : if (is_defined_elsewhere)
2310 227066 : equal_fn->func_declaration_value()->set_asm_name(bname.asm_name());
2311 : else
2312 : {
2313 92581 : if (gogo->in_global_scope())
2314 92506 : this->write_equal_function(gogo, name, size, &bname, equal_fntype);
2315 : else
2316 75 : gogo->queue_equal_function(this, name, size, &bname, equal_fntype);
2317 : }
2318 :
2319 319647 : return equal_fn;
2320 319647 : }
2321 :
2322 : // Write the equal function for a type that needs it written
2323 : // specially.
2324 :
2325 : void
2326 92581 : Type::write_equal_function(Gogo* gogo, Named_type* name, int64_t size,
2327 : const Backend_name* bname,
2328 : Function_type* equal_fntype)
2329 : {
2330 92581 : Location bloc = Linemap::predeclared_location();
2331 :
2332 92581 : if (gogo->specific_type_functions_are_written())
2333 : {
2334 0 : go_assert(saw_errors());
2335 0 : return;
2336 : }
2337 :
2338 92581 : go_assert(this->is_comparable());
2339 :
2340 92581 : Named_object* equal_fn = gogo->start_function(bname->name(), equal_fntype,
2341 : false, bloc);
2342 92581 : equal_fn->func_value()->set_asm_name(bname->asm_name());
2343 92581 : equal_fn->func_value()->set_is_type_specific_function();
2344 92581 : gogo->start_block(bloc);
2345 :
2346 92581 : if (size != -1)
2347 71505 : this->write_identity_equal(gogo, equal_fn, size);
2348 21076 : else if (name != NULL && name->real_type()->named_type() != NULL)
2349 80 : this->write_named_equal(gogo, equal_fn, name);
2350 20996 : else if (this->struct_type() != NULL)
2351 24674 : this->struct_type()->write_equal_function(gogo, equal_fn, name);
2352 8659 : else if (this->array_type() != NULL)
2353 17318 : this->array_type()->write_equal_function(gogo, equal_fn, name);
2354 : else
2355 0 : go_unreachable();
2356 :
2357 92581 : Block* b = gogo->finish_block(bloc);
2358 92581 : gogo->add_block(b, bloc);
2359 92581 : b->determine_types(gogo);
2360 92581 : gogo->lower_block(equal_fn, b);
2361 92581 : gogo->order_block(b);
2362 92581 : gogo->remove_shortcuts_in_block(b);
2363 92581 : gogo->finish_function(bloc);
2364 :
2365 : // Build the function descriptor for the type descriptor to refer to.
2366 92581 : equal_fn->func_value()->descriptor(gogo, equal_fn);
2367 : }
2368 :
2369 : // Write an equality function for a type that can use an identity
2370 : // equality comparison but is not one of the standard supported sizes.
2371 : // For example, this would be used for the type [3]byte. This builds
2372 : // a return statement that returns a call to the memequal function,
2373 : // passing the two keys from the function arguments (already
2374 : // constructed before this is called), and the constant size.
2375 :
2376 : void
2377 71505 : Type::write_identity_equal(Gogo* gogo, Named_object* function, int64_t size)
2378 : {
2379 71505 : Location bloc = Linemap::predeclared_location();
2380 :
2381 71505 : Type* unsafe_pointer_type = Type::make_pointer_type(Type::make_void_type());
2382 71505 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
2383 :
2384 71505 : Typed_identifier_list* params = new Typed_identifier_list();
2385 143010 : params->push_back(Typed_identifier("key1", unsafe_pointer_type, bloc));
2386 143010 : params->push_back(Typed_identifier("key2", unsafe_pointer_type, bloc));
2387 143010 : params->push_back(Typed_identifier("size", uintptr_type, bloc));
2388 :
2389 71505 : Typed_identifier_list* results = new Typed_identifier_list();
2390 143010 : results->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
2391 :
2392 71505 : Function_type* memequal_fntype = Type::make_function_type(NULL, params,
2393 : results, bloc);
2394 :
2395 71505 : Named_object* memequal =
2396 71505 : Named_object::make_function_declaration("runtime.memequal", NULL,
2397 : memequal_fntype, bloc);
2398 71505 : memequal->func_declaration_value()->set_asm_name("runtime.memequal");
2399 :
2400 71505 : Named_object* key1_arg = gogo->lookup("key1", NULL);
2401 71505 : go_assert(key1_arg != NULL);
2402 71505 : Named_object* key2_arg = gogo->lookup("key2", NULL);
2403 71505 : go_assert(key2_arg != NULL);
2404 :
2405 71505 : Expression* key1_ref = Expression::make_var_reference(key1_arg, bloc);
2406 71505 : Expression* key2_ref = Expression::make_var_reference(key2_arg, bloc);
2407 71505 : Expression* size_arg = Expression::make_integer_int64(size, uintptr_type,
2408 : bloc);
2409 71505 : Expression_list* args = new Expression_list();
2410 71505 : args->push_back(key1_ref);
2411 71505 : args->push_back(key2_ref);
2412 71505 : args->push_back(size_arg);
2413 71505 : Expression* func = Expression::make_func_reference(memequal, NULL, bloc);
2414 71505 : Expression* call = Expression::make_call(func, args, false, bloc);
2415 :
2416 71505 : Expression_list* vals = new Expression_list();
2417 71505 : vals->push_back(call);
2418 71505 : Statement* s = Statement::make_return_statement(function, vals, bloc);
2419 71505 : s->determine_types(gogo);
2420 71505 : gogo->add_statement(s);
2421 71505 : }
2422 :
2423 : // Write an equality function that simply calls the equality function
2424 : // for a named type. This is used when one named type is defined as
2425 : // another. This ensures that this case works when the other named
2426 : // type is defined in another package and relies on calling equality
2427 : // functions defined only in that package.
2428 :
2429 : void
2430 80 : Type::write_named_equal(Gogo* gogo, Named_object* function, Named_type* name)
2431 : {
2432 80 : Location bloc = Linemap::predeclared_location();
2433 :
2434 : // The pointers to the types we are going to compare. These have
2435 : // type unsafe.Pointer.
2436 80 : Named_object* key1_arg = gogo->lookup("key1", NULL);
2437 80 : Named_object* key2_arg = gogo->lookup("key2", NULL);
2438 80 : go_assert(key1_arg != NULL && key2_arg != NULL);
2439 :
2440 80 : Named_type* base_type = name->real_type()->named_type();
2441 80 : go_assert(base_type != NULL);
2442 :
2443 : // Build temporaries with the base type.
2444 80 : Type* pt = Type::make_pointer_type(base_type);
2445 :
2446 80 : Expression* ref = Expression::make_var_reference(key1_arg, bloc);
2447 80 : ref = Expression::make_cast(pt, ref, bloc);
2448 80 : Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
2449 80 : p1->determine_types(gogo);
2450 80 : gogo->add_statement(p1);
2451 :
2452 80 : ref = Expression::make_var_reference(key2_arg, bloc);
2453 80 : ref = Expression::make_cast(pt, ref, bloc);
2454 80 : Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
2455 80 : p2->determine_types(gogo);
2456 80 : gogo->add_statement(p2);
2457 :
2458 : // Compare the values for equality.
2459 80 : Expression* t1 = Expression::make_temporary_reference(p1, bloc);
2460 80 : t1 = Expression::make_dereference(t1, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2461 :
2462 80 : Expression* t2 = Expression::make_temporary_reference(p2, bloc);
2463 80 : t2 = Expression::make_dereference(t2, Expression::NIL_CHECK_NOT_NEEDED, bloc);
2464 :
2465 80 : Expression* cond = Expression::make_binary(OPERATOR_EQEQ, t1, t2, bloc);
2466 :
2467 : // Return the equality comparison.
2468 80 : Expression_list* vals = new Expression_list();
2469 80 : vals->push_back(cond);
2470 80 : Statement* s = Statement::make_return_statement(function, vals, bloc);
2471 80 : s->determine_types(gogo);
2472 80 : gogo->add_statement(s);
2473 80 : }
2474 :
2475 : // Return whether this type is stored directly in an interface's
2476 : // data word.
2477 : //
2478 : // Since finalize_methods runs before type checking, we may see a
2479 : // malformed type like 'type T struct { x T }'. Use a visited map
2480 : // to avoid infinite recursion.
2481 :
2482 : bool
2483 3024315 : Type::is_direct_iface_type() const
2484 : {
2485 3024315 : Unordered_set(const Type*) visited;
2486 3024315 : return this->is_direct_iface_type_helper(&visited);
2487 3024315 : }
2488 :
2489 : bool
2490 3163556 : Type::is_direct_iface_type_helper(Unordered_set(const Type*)* visited) const
2491 : {
2492 3163556 : if (this->points_to() != NULL)
2493 : {
2494 : // Pointers to notinheap types must be stored indirectly. See
2495 : // https://golang.org/issue/42076.
2496 783876 : if (!this->points_to()->in_heap())
2497 : return false;
2498 : return true;
2499 : }
2500 :
2501 4756613 : if (this->channel_type() != NULL
2502 2396592 : || this->function_type() != NULL
2503 138678 : || this->map_type() != NULL)
2504 138678 : return true;
2505 :
2506 2241002 : std::pair<Unordered_set(const Type*)::iterator, bool> ins
2507 2241002 : = visited->insert(this);
2508 2241002 : if (!ins.second)
2509 : // malformed circular type
2510 : return false;
2511 :
2512 2240993 : const Struct_type* st = this->struct_type();
2513 2240993 : if (st != NULL)
2514 984139 : return (st->field_count() == 1
2515 1097390 : && st->field(0)->type()->is_direct_iface_type_helper(visited));
2516 1256854 : const Array_type* at = this->array_type();
2517 200672 : if (at != NULL && !at->is_slice_type())
2518 : {
2519 119191 : int64_t len;
2520 238374 : return (at->int_length(&len) && len == 1
2521 145181 : && at->element_type()->is_direct_iface_type_helper(visited));
2522 : }
2523 : return false;
2524 : }
2525 :
2526 : // Return a composite literal for the type descriptor for a plain type
2527 : // of kind RUNTIME_TYPE_KIND named NAME.
2528 :
2529 : Expression*
2530 283603 : Type::type_descriptor_constructor(Gogo* gogo, int runtime_type_kind,
2531 : Named_type* name, const Methods* methods,
2532 : bool only_value_methods)
2533 : {
2534 283603 : Location bloc = Linemap::predeclared_location();
2535 :
2536 283603 : Type* td_type = Type::make_type_descriptor_type();
2537 567206 : const Struct_field_list* fields = td_type->struct_type()->fields();
2538 :
2539 283603 : Expression_list* vals = new Expression_list();
2540 283603 : vals->reserve(12);
2541 :
2542 283603 : bool has_pointer;
2543 283603 : if (name != NULL)
2544 62953 : has_pointer = name->has_pointer();
2545 : else
2546 220650 : has_pointer = this->has_pointer();
2547 283603 : if (!has_pointer)
2548 39926 : runtime_type_kind |= RUNTIME_TYPE_KIND_NO_POINTERS;
2549 283603 : if (this->is_direct_iface_type())
2550 177535 : runtime_type_kind |= RUNTIME_TYPE_KIND_DIRECT_IFACE;
2551 283603 : int64_t ptrsize;
2552 283603 : int64_t ptrdata;
2553 283603 : if (has_pointer && this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2554 82 : runtime_type_kind |= RUNTIME_TYPE_KIND_GC_PROG;
2555 :
2556 283603 : Struct_field_list::const_iterator p = fields->begin();
2557 283603 : go_assert(p->is_field_name("size"));
2558 283603 : Expression::Type_info type_info = Expression::TYPE_INFO_SIZE;
2559 283603 : vals->push_back(Expression::make_type_info(this, type_info));
2560 :
2561 283603 : ++p;
2562 283603 : go_assert(p->is_field_name("ptrdata"));
2563 283603 : type_info = Expression::TYPE_INFO_DESCRIPTOR_PTRDATA;
2564 283603 : if (has_pointer)
2565 243677 : vals->push_back(Expression::make_type_info(this, type_info));
2566 : else
2567 39926 : vals->push_back(Expression::make_integer_ul(0, p->type(), bloc));
2568 :
2569 283603 : ++p;
2570 283603 : go_assert(p->is_field_name("hash"));
2571 283603 : unsigned int h;
2572 283603 : if (name != NULL)
2573 62953 : h = name->hash_for_method(gogo, Type::COMPARE_TAGS);
2574 : else
2575 220650 : h = this->hash_for_method(gogo, Type::COMPARE_TAGS);
2576 283603 : vals->push_back(Expression::make_integer_ul(h, p->type(), bloc));
2577 :
2578 283603 : ++p;
2579 283603 : go_assert(p->is_field_name("tflag"));
2580 283603 : unsigned long tflag = 0;
2581 283603 : if (this->compare_is_identity(gogo))
2582 125111 : tflag |= TFLAG_REGULAR_MEMORY;
2583 283603 : vals->push_back(Expression::make_integer_ul(tflag, p->type(), bloc));
2584 :
2585 283603 : ++p;
2586 283603 : go_assert(p->is_field_name("align"));
2587 283603 : type_info = Expression::TYPE_INFO_ALIGNMENT;
2588 283603 : vals->push_back(Expression::make_type_info(this, type_info));
2589 :
2590 283603 : ++p;
2591 283603 : go_assert(p->is_field_name("fieldAlign"));
2592 283603 : type_info = Expression::TYPE_INFO_FIELD_ALIGNMENT;
2593 283603 : vals->push_back(Expression::make_type_info(this, type_info));
2594 :
2595 283603 : ++p;
2596 283603 : go_assert(p->is_field_name("kind"));
2597 283603 : vals->push_back(Expression::make_integer_ul(runtime_type_kind, p->type(),
2598 : bloc));
2599 :
2600 283603 : ++p;
2601 283603 : go_assert(p->is_field_name("equal"));
2602 283603 : Function_type* equal_fntype = p->type()->function_type();
2603 283603 : Named_object* equal_fn = this->equal_function(gogo, name, equal_fntype);
2604 283603 : if (equal_fn == NULL)
2605 149244 : vals->push_back(Expression::make_cast(equal_fntype,
2606 : Expression::make_nil(bloc),
2607 : bloc));
2608 : else
2609 134359 : vals->push_back(Expression::make_func_reference(equal_fn, NULL, bloc));
2610 :
2611 283603 : ++p;
2612 283603 : go_assert(p->is_field_name("gcdata"));
2613 283603 : if (has_pointer)
2614 243677 : vals->push_back(Expression::make_gc_symbol(this));
2615 : else
2616 39926 : vals->push_back(Expression::make_cast(p->type(),
2617 : Expression::make_nil(bloc),
2618 : bloc));
2619 :
2620 283603 : ++p;
2621 283603 : go_assert(p->is_field_name("string"));
2622 283603 : Expression* s = Expression::make_string((name != NULL
2623 283603 : ? name->reflection(gogo)
2624 : : this->reflection(gogo)),
2625 : bloc);
2626 283603 : vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
2627 :
2628 283603 : ++p;
2629 283603 : go_assert(p->is_field_name("uncommonType"));
2630 283603 : if (name == NULL && methods == NULL)
2631 191139 : vals->push_back(Expression::make_nil(bloc));
2632 : else
2633 : {
2634 92464 : if (methods == NULL)
2635 62950 : methods = name->methods();
2636 184928 : vals->push_back(this->uncommon_type_constructor(gogo,
2637 : p->type()->deref(),
2638 : name, methods,
2639 : only_value_methods));
2640 : }
2641 :
2642 283603 : ++p;
2643 283603 : go_assert(p->is_field_name("ptrToThis"));
2644 283603 : if (name == NULL && methods == NULL)
2645 191139 : vals->push_back(Expression::make_nil(bloc));
2646 : else
2647 : {
2648 92464 : Type* pt;
2649 92464 : if (name != NULL)
2650 62953 : pt = Type::make_pointer_type(name);
2651 : else
2652 29511 : pt = Type::make_pointer_type(this);
2653 92464 : vals->push_back(Expression::make_type_descriptor(pt, bloc));
2654 : }
2655 :
2656 283603 : ++p;
2657 283603 : go_assert(p == fields->end());
2658 :
2659 283603 : return Expression::make_struct_composite_literal(td_type, vals, bloc);
2660 : }
2661 :
2662 : // The maximum length of a GC ptrmask bitmap. This corresponds to the
2663 : // length used by the gc toolchain, and also appears in
2664 : // libgo/go/reflect/type.go.
2665 :
2666 : static const int64_t max_ptrmask_bytes = 2048;
2667 :
2668 : // Return a pointer to the Garbage Collection information for this type.
2669 :
2670 : Bexpression*
2671 293395 : Type::gc_symbol_pointer(Gogo* gogo)
2672 : {
2673 293395 : Type* t = this->unalias();
2674 :
2675 293395 : if (!t->has_pointer())
2676 13946 : return gogo->backend()->nil_pointer_expression();
2677 :
2678 279449 : if (t->gc_symbol_var_ == NULL)
2679 : {
2680 242111 : t->make_gc_symbol_var(gogo);
2681 242111 : go_assert(t->gc_symbol_var_ != NULL);
2682 : }
2683 279449 : Location bloc = Linemap::predeclared_location();
2684 279449 : Bexpression* var_expr =
2685 279449 : gogo->backend()->var_expression(t->gc_symbol_var_, bloc);
2686 279449 : Bexpression* addr_expr =
2687 279449 : gogo->backend()->address_expression(var_expr, bloc);
2688 :
2689 279449 : Type* uint8_type = Type::lookup_integer_type("uint8");
2690 279449 : Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
2691 279449 : Btype* ubtype = pointer_uint8_type->get_backend(gogo);
2692 279449 : return gogo->backend()->convert_expression(ubtype, addr_expr, bloc);
2693 : }
2694 :
2695 : // A mapping from unnamed types to GC symbol variables.
2696 :
2697 : Type::GC_symbol_vars Type::gc_symbol_vars;
2698 :
2699 : // Build the GC symbol for this type.
2700 :
2701 : void
2702 242111 : Type::make_gc_symbol_var(Gogo* gogo)
2703 : {
2704 242111 : go_assert(this->gc_symbol_var_ == NULL);
2705 :
2706 242111 : Named_type* nt = this->named_type();
2707 :
2708 : // We can have multiple instances of unnamed types and similar to type
2709 : // descriptors, we only want to the emit the GC data once, so we use a
2710 : // hash table.
2711 242111 : Bvariable** phash = NULL;
2712 242111 : if (nt == NULL)
2713 : {
2714 225387 : Bvariable* bvnull = NULL;
2715 225387 : std::pair<GC_symbol_vars::iterator, bool> ins =
2716 225387 : Type::gc_symbol_vars.insert(std::make_pair(this, bvnull));
2717 225387 : if (!ins.second)
2718 : {
2719 : // We've already built a gc symbol for this type.
2720 10285 : this->gc_symbol_var_ = ins.first->second;
2721 10285 : return;
2722 : }
2723 215102 : phash = &ins.first->second;
2724 : }
2725 :
2726 231826 : int64_t ptrsize;
2727 231826 : int64_t ptrdata;
2728 231826 : if (!this->needs_gcprog(gogo, &ptrsize, &ptrdata))
2729 : {
2730 231712 : this->gc_symbol_var_ = this->gc_ptrmask_var(gogo, ptrsize, ptrdata);
2731 231712 : if (phash != NULL)
2732 215022 : *phash = this->gc_symbol_var_;
2733 231712 : return;
2734 : }
2735 :
2736 114 : std::string sym_name = gogo->gc_symbol_name(this);
2737 :
2738 : // Build the contents of the gc symbol.
2739 114 : Expression* sym_init = this->gcprog_constructor(gogo, ptrsize, ptrdata);
2740 114 : Btype* sym_btype = sym_init->type()->get_backend(gogo);
2741 :
2742 : // If the type descriptor for this type is defined somewhere else, so is the
2743 : // GC symbol.
2744 114 : const Package* dummy;
2745 114 : if (this->type_descriptor_defined_elsewhere(nt, &dummy))
2746 : {
2747 0 : this->gc_symbol_var_ =
2748 0 : gogo->backend()->implicit_variable_reference(sym_name, "",
2749 : sym_btype);
2750 0 : if (phash != NULL)
2751 0 : *phash = this->gc_symbol_var_;
2752 0 : return;
2753 : }
2754 :
2755 : // See if this gc symbol can appear in multiple packages.
2756 114 : bool is_common = false;
2757 114 : if (nt != NULL)
2758 : {
2759 : // We create the symbol for a builtin type whenever we need
2760 : // it.
2761 34 : is_common = nt->is_builtin();
2762 : }
2763 : else
2764 : {
2765 : // This is an unnamed type. The descriptor could be defined in
2766 : // any package where it is needed, and the linker will pick one
2767 : // descriptor to keep.
2768 : is_common = true;
2769 : }
2770 :
2771 : // Since we are building the GC symbol in this package, we must create the
2772 : // variable before converting the initializer to its backend representation
2773 : // because the initializer may refer to the GC symbol for this type.
2774 34 : unsigned int flags = Backend::variable_is_constant;
2775 34 : if (is_common)
2776 : flags |= Backend::variable_is_common;
2777 : else
2778 114 : flags |= Backend::variable_is_hidden;
2779 228 : this->gc_symbol_var_ =
2780 114 : gogo->backend()->implicit_variable(sym_name, "", sym_btype, flags, 0);
2781 114 : if (phash != NULL)
2782 80 : *phash = this->gc_symbol_var_;
2783 :
2784 114 : Translate_context context(gogo, NULL, NULL, NULL);
2785 114 : context.set_is_const();
2786 114 : Bexpression* sym_binit = sym_init->get_backend(&context);
2787 114 : gogo->backend()->implicit_variable_set_init(this->gc_symbol_var_, sym_name,
2788 : sym_btype, flags, sym_binit);
2789 114 : }
2790 :
2791 : // Return whether this type needs a GC program, and set *PTRDATA to
2792 : // the size of the pointer data in bytes and *PTRSIZE to the size of a
2793 : // pointer.
2794 :
2795 : bool
2796 710195 : Type::needs_gcprog(Gogo* gogo, int64_t* ptrsize, int64_t* ptrdata)
2797 : {
2798 710195 : Type* voidptr = Type::make_pointer_type(Type::make_void_type());
2799 710195 : if (!voidptr->backend_type_size(gogo, ptrsize))
2800 0 : go_unreachable();
2801 :
2802 710195 : if (!this->backend_type_ptrdata(gogo, ptrdata))
2803 : {
2804 0 : go_assert(saw_errors());
2805 : return false;
2806 : }
2807 :
2808 710195 : return *ptrdata / *ptrsize > max_ptrmask_bytes;
2809 : }
2810 :
2811 : // A simple class used to build a GC ptrmask for a type.
2812 :
2813 463450 : class Ptrmask
2814 : {
2815 : public:
2816 231725 : Ptrmask(size_t count)
2817 231725 : : bits_((count + 7) / 8, 0)
2818 231725 : {}
2819 :
2820 : void
2821 : set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
2822 :
2823 : std::string
2824 : symname() const;
2825 :
2826 : Expression*
2827 : constructor() const;
2828 :
2829 : private:
2830 : void
2831 608732 : set(size_t index)
2832 608732 : { this->bits_.at(index / 8) |= 1 << (index % 8); }
2833 :
2834 : // The actual bits.
2835 : std::vector<unsigned char> bits_;
2836 : };
2837 :
2838 : // Set bits in ptrmask starting from OFFSET based on TYPE. OFFSET
2839 : // counts in bytes. PTRSIZE is the size of a pointer on the target
2840 : // system.
2841 :
2842 : void
2843 822130 : Ptrmask::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
2844 : {
2845 822130 : if (!type->has_pointer())
2846 : return;
2847 :
2848 698661 : switch (type->base()->classification())
2849 : {
2850 0 : default:
2851 0 : case Type::TYPE_NIL:
2852 0 : case Type::TYPE_CALL_MULTIPLE_RESULT:
2853 0 : case Type::TYPE_NAMED:
2854 0 : case Type::TYPE_FORWARD:
2855 0 : go_unreachable();
2856 :
2857 : case Type::TYPE_ERROR:
2858 : case Type::TYPE_VOID:
2859 : case Type::TYPE_BOOLEAN:
2860 : case Type::TYPE_INTEGER:
2861 : case Type::TYPE_FLOAT:
2862 : case Type::TYPE_COMPLEX:
2863 : case Type::TYPE_SINK:
2864 : break;
2865 :
2866 302148 : case Type::TYPE_FUNCTION:
2867 302148 : case Type::TYPE_POINTER:
2868 302148 : case Type::TYPE_MAP:
2869 302148 : case Type::TYPE_CHANNEL:
2870 : // These types are all a single pointer.
2871 302148 : go_assert((offset % ptrsize) == 0);
2872 302148 : this->set(offset / ptrsize);
2873 302148 : break;
2874 :
2875 146229 : case Type::TYPE_STRING:
2876 : // A string starts with a single pointer.
2877 146229 : go_assert((offset % ptrsize) == 0);
2878 146229 : this->set(offset / ptrsize);
2879 146229 : break;
2880 :
2881 50256 : case Type::TYPE_INTERFACE:
2882 : // An interface is two pointers.
2883 50256 : go_assert((offset % ptrsize) == 0);
2884 50256 : this->set(offset / ptrsize);
2885 50256 : this->set((offset / ptrsize) + 1);
2886 50256 : break;
2887 :
2888 114608 : case Type::TYPE_STRUCT:
2889 114608 : {
2890 229216 : const Struct_field_list* fields = type->struct_type()->fields();
2891 114608 : int64_t soffset = 0;
2892 114608 : for (Struct_field_list::const_iterator pf = fields->begin();
2893 499091 : pf != fields->end();
2894 384483 : ++pf)
2895 : {
2896 384483 : int64_t field_align;
2897 384483 : if (!pf->type()->backend_type_field_align(gogo, &field_align))
2898 : {
2899 0 : go_assert(saw_errors());
2900 0 : return;
2901 : }
2902 384483 : soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
2903 :
2904 384483 : this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
2905 :
2906 384483 : int64_t field_size;
2907 384483 : if (!pf->type()->backend_type_size(gogo, &field_size))
2908 : {
2909 0 : go_assert(saw_errors());
2910 : return;
2911 : }
2912 384483 : soffset += field_size;
2913 : }
2914 : }
2915 : break;
2916 :
2917 85420 : case Type::TYPE_ARRAY:
2918 85420 : if (type->is_slice_type())
2919 : {
2920 : // A slice starts with a single pointer.
2921 59843 : go_assert((offset % ptrsize) == 0);
2922 59843 : this->set(offset / ptrsize);
2923 59843 : break;
2924 : }
2925 : else
2926 : {
2927 25577 : int64_t len;
2928 51154 : if (!type->array_type()->int_length(&len))
2929 : {
2930 0 : go_assert(saw_errors());
2931 0 : return;
2932 : }
2933 :
2934 51154 : Type* element_type = type->array_type()->element_type();
2935 25577 : int64_t ele_size;
2936 25577 : if (!element_type->backend_type_size(gogo, &ele_size))
2937 : {
2938 0 : go_assert(saw_errors());
2939 : return;
2940 : }
2941 :
2942 : int64_t eoffset = 0;
2943 231501 : for (int64_t i = 0; i < len; i++, eoffset += ele_size)
2944 205924 : this->set_from(gogo, element_type, ptrsize, offset + eoffset);
2945 25577 : break;
2946 : }
2947 : }
2948 : }
2949 :
2950 : // Return a symbol name for this ptrmask. This is used to coalesce
2951 : // identical ptrmasks, which are common. The symbol name must use
2952 : // only characters that are valid in symbols. It's nice if it's
2953 : // short. For smaller ptrmasks, we convert it to a string that uses
2954 : // only 32 characters. For longer pointer masks, apply the same
2955 : // process to the SHA1 digest of the bits, so as to avoid
2956 : // pathologically long symbol names (see related Go issues #32083 and
2957 : // #11583 for more on this). To avoid collisions between the two
2958 : // encoding schemes, use a prefix ("X") for the SHA form to
2959 : // disambiguate.
2960 :
2961 : std::string
2962 231725 : Ptrmask::symname() const
2963 : {
2964 231725 : const std::vector<unsigned char>* bits(&this->bits_);
2965 231725 : std::vector<unsigned char> shabits;
2966 231725 : std::string prefix;
2967 :
2968 231725 : if (this->bits_.size() > 128)
2969 : {
2970 : // Produce a SHA1 digest of the data.
2971 76 : Go_sha1_helper* sha1_helper = go_create_sha1_helper();
2972 76 : sha1_helper->process_bytes(&this->bits_[0], this->bits_.size());
2973 76 : std::string digest = sha1_helper->finish();
2974 76 : delete sha1_helper;
2975 :
2976 : // Redirect the bits vector to the digest, and update the prefix.
2977 76 : prefix = "X";
2978 1596 : for (std::string::const_iterator p = digest.begin();
2979 1596 : p != digest.end();
2980 1520 : ++p)
2981 : {
2982 1520 : unsigned char c = *p;
2983 1520 : shabits.push_back(c);
2984 : }
2985 76 : bits = &shabits;
2986 76 : }
2987 :
2988 231725 : const char chars[33] = "abcdefghijklmnopqrstuvwxyzABCDEF";
2989 231725 : go_assert(chars[32] == '\0');
2990 231725 : std::string ret(prefix);
2991 231725 : unsigned int b = 0;
2992 231725 : int remaining = 0;
2993 536497 : for (std::vector<unsigned char>::const_iterator p = bits->begin();
2994 536497 : p != bits->end();
2995 304772 : ++p)
2996 : {
2997 304772 : b |= *p << remaining;
2998 304772 : remaining += 8;
2999 656564 : while (remaining >= 5)
3000 : {
3001 351792 : ret += chars[b & 0x1f];
3002 351792 : b >>= 5;
3003 351792 : remaining -= 5;
3004 : }
3005 : }
3006 461906 : while (remaining > 0)
3007 : {
3008 230181 : ret += chars[b & 0x1f];
3009 230181 : b >>= 5;
3010 230181 : remaining -= 5;
3011 : }
3012 231725 : return ret;
3013 231725 : }
3014 :
3015 : // Return a constructor for this ptrmask. This will be used to
3016 : // initialize the runtime ptrmask value.
3017 :
3018 : Expression*
3019 23547 : Ptrmask::constructor() const
3020 : {
3021 23547 : Location bloc = Linemap::predeclared_location();
3022 23547 : Type* byte_type = Type::lookup_integer_type("byte");
3023 23547 : Expression* len = Expression::make_integer_ul(this->bits_.size(), NULL,
3024 : bloc);
3025 23547 : Array_type* at = Type::make_array_type(byte_type, len);
3026 23547 : Expression_list* vals = new Expression_list();
3027 23547 : vals->reserve(this->bits_.size());
3028 23547 : for (std::vector<unsigned char>::const_iterator p = this->bits_.begin();
3029 309438 : p != this->bits_.end();
3030 285891 : ++p)
3031 285891 : vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
3032 23547 : return Expression::make_array_composite_literal(at, vals, bloc);
3033 : }
3034 :
3035 : // The hash table mapping a ptrmask symbol name to the ptrmask variable.
3036 : Type::GC_gcbits_vars Type::gc_gcbits_vars;
3037 :
3038 : // Return a ptrmask variable for a type. For a type descriptor this
3039 : // is only used for variables that are small enough to not need a
3040 : // gcprog, but for a global variable this is used for a variable of
3041 : // any size. PTRDATA is the number of bytes of the type that contain
3042 : // pointer data. PTRSIZE is the size of a pointer on the target
3043 : // system.
3044 :
3045 : Bvariable*
3046 231725 : Type::gc_ptrmask_var(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
3047 : {
3048 231725 : Ptrmask ptrmask(ptrdata / ptrsize);
3049 231725 : if (ptrdata >= ptrsize)
3050 231723 : ptrmask.set_from(gogo, this, ptrsize, 0);
3051 : else
3052 : {
3053 : // This can happen in error cases. Just build an empty gcbits.
3054 2 : go_assert(saw_errors());
3055 : }
3056 :
3057 231725 : std::string sym_name = gogo->ptrmask_symbol_name(ptrmask.symname());
3058 231725 : Bvariable* bvnull = NULL;
3059 231725 : std::pair<GC_gcbits_vars::iterator, bool> ins =
3060 231725 : Type::gc_gcbits_vars.insert(std::make_pair(sym_name, bvnull));
3061 231725 : if (!ins.second)
3062 : {
3063 : // We've already built a GC symbol for this set of gcbits.
3064 208178 : return ins.first->second;
3065 : }
3066 :
3067 23547 : Expression* val = ptrmask.constructor();
3068 23547 : Translate_context context(gogo, NULL, NULL, NULL);
3069 23547 : context.set_is_const();
3070 23547 : Bexpression* bval = val->get_backend(&context);
3071 :
3072 23547 : Btype *btype = val->type()->get_backend(gogo);
3073 23547 : unsigned int flags = (Backend::variable_is_constant
3074 : | Backend::variable_is_common);
3075 23547 : Bvariable* ret = gogo->backend()->implicit_variable(sym_name, "",
3076 : btype, flags, 0);
3077 23547 : gogo->backend()->implicit_variable_set_init(ret, sym_name, btype, flags,
3078 : bval);
3079 23547 : ins.first->second = ret;
3080 23547 : return ret;
3081 231725 : }
3082 :
3083 : // A GCProg is used to build a program for the garbage collector.
3084 : // This is used for types with a lot of pointer data, to reduce the
3085 : // size of the data in the compiled program. The program is expanded
3086 : // at runtime. For the format, see runGCProg in libgo/go/runtime/mbitmap.go.
3087 :
3088 196 : class GCProg
3089 : {
3090 : public:
3091 196 : GCProg()
3092 196 : : bytes_(), index_(0), nb_(0)
3093 : {}
3094 :
3095 : // The number of bits described so far.
3096 : int64_t
3097 196 : bit_index() const
3098 196 : { return this->index_; }
3099 :
3100 : void
3101 : set_from(Gogo*, Type*, int64_t ptrsize, int64_t offset);
3102 :
3103 : void
3104 : end();
3105 :
3106 : Expression*
3107 : constructor() const;
3108 :
3109 : private:
3110 : void
3111 : ptr(int64_t);
3112 :
3113 : bool
3114 : should_repeat(int64_t, int64_t);
3115 :
3116 : void
3117 : repeat(int64_t, int64_t);
3118 :
3119 : void
3120 : zero_until(int64_t);
3121 :
3122 : void
3123 : lit(unsigned char);
3124 :
3125 : void
3126 : varint(int64_t);
3127 :
3128 : void
3129 : flushlit();
3130 :
3131 : // Add a byte to the program.
3132 : void
3133 1719 : byte(unsigned char x)
3134 1719 : { this->bytes_.push_back(x); }
3135 :
3136 : // The maximum number of bytes of literal bits.
3137 : static const int max_literal = 127;
3138 :
3139 : // The program.
3140 : std::vector<unsigned char> bytes_;
3141 : // The index of the last bit described.
3142 : int64_t index_;
3143 : // The current set of literal bits.
3144 : unsigned char b_[max_literal];
3145 : // The current number of literal bits.
3146 : int nb_;
3147 : };
3148 :
3149 : // Set data in gcprog starting from OFFSET based on TYPE. OFFSET
3150 : // counts in bytes. PTRSIZE is the size of a pointer on the target
3151 : // system.
3152 :
3153 : void
3154 2344 : GCProg::set_from(Gogo* gogo, Type* type, int64_t ptrsize, int64_t offset)
3155 : {
3156 2344 : switch (type->base()->classification())
3157 : {
3158 0 : default:
3159 0 : case Type::TYPE_NIL:
3160 0 : case Type::TYPE_CALL_MULTIPLE_RESULT:
3161 0 : case Type::TYPE_NAMED:
3162 0 : case Type::TYPE_FORWARD:
3163 0 : go_unreachable();
3164 :
3165 : case Type::TYPE_ERROR:
3166 : case Type::TYPE_VOID:
3167 : case Type::TYPE_BOOLEAN:
3168 : case Type::TYPE_INTEGER:
3169 : case Type::TYPE_FLOAT:
3170 : case Type::TYPE_COMPLEX:
3171 : case Type::TYPE_SINK:
3172 : break;
3173 :
3174 409 : case Type::TYPE_FUNCTION:
3175 409 : case Type::TYPE_POINTER:
3176 409 : case Type::TYPE_MAP:
3177 409 : case Type::TYPE_CHANNEL:
3178 : // These types are all a single pointer.
3179 409 : go_assert((offset % ptrsize) == 0);
3180 409 : this->ptr(offset / ptrsize);
3181 409 : break;
3182 :
3183 50 : case Type::TYPE_STRING:
3184 : // A string starts with a single pointer.
3185 50 : go_assert((offset % ptrsize) == 0);
3186 50 : this->ptr(offset / ptrsize);
3187 50 : break;
3188 :
3189 96 : case Type::TYPE_INTERFACE:
3190 : // An interface is two pointers.
3191 96 : go_assert((offset % ptrsize) == 0);
3192 96 : this->ptr(offset / ptrsize);
3193 96 : this->ptr((offset / ptrsize) + 1);
3194 96 : break;
3195 :
3196 346 : case Type::TYPE_STRUCT:
3197 346 : {
3198 346 : if (!type->has_pointer())
3199 : return;
3200 :
3201 492 : const Struct_field_list* fields = type->struct_type()->fields();
3202 246 : int64_t soffset = 0;
3203 246 : for (Struct_field_list::const_iterator pf = fields->begin();
3204 2209 : pf != fields->end();
3205 1963 : ++pf)
3206 : {
3207 1963 : int64_t field_align;
3208 1963 : if (!pf->type()->backend_type_field_align(gogo, &field_align))
3209 : {
3210 0 : go_assert(saw_errors());
3211 0 : return;
3212 : }
3213 1963 : soffset = (soffset + (field_align - 1)) &~ (field_align - 1);
3214 :
3215 1963 : this->set_from(gogo, pf->type(), ptrsize, offset + soffset);
3216 :
3217 1963 : int64_t field_size;
3218 1963 : if (!pf->type()->backend_type_size(gogo, &field_size))
3219 : {
3220 0 : go_assert(saw_errors());
3221 : return;
3222 : }
3223 1963 : soffset += field_size;
3224 : }
3225 : }
3226 : break;
3227 :
3228 635 : case Type::TYPE_ARRAY:
3229 635 : if (type->is_slice_type())
3230 : {
3231 : // A slice starts with a single pointer.
3232 294 : go_assert((offset % ptrsize) == 0);
3233 294 : this->ptr(offset / ptrsize);
3234 294 : break;
3235 : }
3236 : else
3237 : {
3238 341 : if (!type->has_pointer())
3239 220 : return;
3240 :
3241 121 : int64_t len;
3242 242 : if (!type->array_type()->int_length(&len))
3243 : {
3244 0 : go_assert(saw_errors());
3245 : return;
3246 : }
3247 :
3248 242 : Type* element_type = type->array_type()->element_type();
3249 :
3250 : // Flatten array of array to a big array by multiplying counts.
3251 121 : while (element_type->array_type() != NULL
3252 158 : && !element_type->is_slice_type())
3253 : {
3254 8 : int64_t ele_len;
3255 16 : if (!element_type->array_type()->int_length(&ele_len))
3256 : {
3257 0 : go_assert(saw_errors());
3258 0 : return;
3259 : }
3260 :
3261 8 : len *= ele_len;
3262 16 : element_type = element_type->array_type()->element_type();
3263 : }
3264 :
3265 121 : int64_t ele_size;
3266 121 : if (!element_type->backend_type_size(gogo, &ele_size))
3267 : {
3268 0 : go_assert(saw_errors());
3269 : return;
3270 : }
3271 :
3272 121 : go_assert(len > 0 && ele_size > 0);
3273 :
3274 121 : if (!this->should_repeat(ele_size / ptrsize, len))
3275 : {
3276 : // Cheaper to just emit the bits.
3277 : int64_t eoffset = 0;
3278 96 : for (int64_t i = 0; i < len; i++, eoffset += ele_size)
3279 80 : this->set_from(gogo, element_type, ptrsize, offset + eoffset);
3280 : }
3281 : else
3282 : {
3283 105 : go_assert((offset % ptrsize) == 0);
3284 105 : go_assert((ele_size % ptrsize) == 0);
3285 105 : this->set_from(gogo, element_type, ptrsize, offset);
3286 105 : this->zero_until((offset + ele_size) / ptrsize);
3287 105 : this->repeat(ele_size / ptrsize, len - 1);
3288 : }
3289 :
3290 121 : break;
3291 : }
3292 : }
3293 : }
3294 :
3295 : // Emit a 1 into the bit stream of a GC program at the given bit index.
3296 :
3297 : void
3298 945 : GCProg::ptr(int64_t index)
3299 : {
3300 945 : go_assert(index >= this->index_);
3301 945 : this->zero_until(index);
3302 945 : this->lit(1);
3303 945 : }
3304 :
3305 : // Return whether it is worthwhile to use a repeat to describe c
3306 : // elements of n bits each, compared to just emitting c copies of the
3307 : // n-bit description.
3308 :
3309 : bool
3310 121 : GCProg::should_repeat(int64_t n, int64_t c)
3311 : {
3312 : // Repeat if there is more than 1 item and if the total data doesn't
3313 : // fit into four bytes.
3314 121 : return c > 1 && c * n > 4 * 8;
3315 : }
3316 :
3317 : // Emit an instruction to repeat the description of the last n words c
3318 : // times (including the initial description, so c + 1 times in total).
3319 :
3320 : void
3321 228 : GCProg::repeat(int64_t n, int64_t c)
3322 : {
3323 228 : if (n == 0 || c == 0)
3324 : return;
3325 228 : this->flushlit();
3326 228 : if (n < 128)
3327 228 : this->byte(0x80 | static_cast<unsigned char>(n & 0x7f));
3328 : else
3329 : {
3330 0 : this->byte(0x80);
3331 0 : this->varint(n);
3332 : }
3333 228 : this->varint(c);
3334 228 : this->index_ += n * c;
3335 : }
3336 :
3337 : // Add zeros to the bit stream up to the given index.
3338 :
3339 : void
3340 1050 : GCProg::zero_until(int64_t index)
3341 : {
3342 1050 : go_assert(index >= this->index_);
3343 1050 : int64_t skip = index - this->index_;
3344 1050 : if (skip == 0)
3345 : return;
3346 580 : if (skip < 4 * 8)
3347 : {
3348 1825 : for (int64_t i = 0; i < skip; ++i)
3349 1368 : this->lit(0);
3350 : return;
3351 : }
3352 123 : this->lit(0);
3353 123 : this->flushlit();
3354 123 : this->repeat(1, skip - 1);
3355 : }
3356 :
3357 : // Add a single literal bit to the program.
3358 :
3359 : void
3360 2436 : GCProg::lit(unsigned char x)
3361 : {
3362 2436 : if (this->nb_ == GCProg::max_literal)
3363 0 : this->flushlit();
3364 2436 : this->b_[this->nb_] = x;
3365 2436 : ++this->nb_;
3366 2436 : ++this->index_;
3367 2436 : }
3368 :
3369 : // Emit the varint encoding of x.
3370 :
3371 : void
3372 228 : GCProg::varint(int64_t x)
3373 : {
3374 228 : go_assert(x >= 0);
3375 624 : while (x >= 0x80)
3376 : {
3377 396 : this->byte(0x80 | static_cast<unsigned char>(x & 0x7f));
3378 396 : x >>= 7;
3379 : }
3380 228 : this->byte(static_cast<unsigned char>(x & 0x7f));
3381 228 : }
3382 :
3383 : // Flush any pending literal bits.
3384 :
3385 : void
3386 465 : GCProg::flushlit()
3387 : {
3388 465 : if (this->nb_ == 0)
3389 : return;
3390 302 : this->byte(static_cast<unsigned char>(this->nb_));
3391 302 : unsigned char bits = 0;
3392 2492 : for (int i = 0; i < this->nb_; ++i)
3393 : {
3394 2190 : bits |= this->b_[i] << (i % 8);
3395 2190 : if ((i + 1) % 8 == 0)
3396 : {
3397 190 : this->byte(bits);
3398 190 : bits = 0;
3399 : }
3400 : }
3401 302 : if (this->nb_ % 8 != 0)
3402 261 : this->byte(bits);
3403 302 : this->nb_ = 0;
3404 : }
3405 :
3406 : // Mark the end of a GC program.
3407 :
3408 : void
3409 114 : GCProg::end()
3410 : {
3411 114 : this->flushlit();
3412 114 : this->byte(0);
3413 114 : }
3414 :
3415 : // Return an Expression for the bytes in a GC program.
3416 :
3417 : Expression*
3418 114 : GCProg::constructor() const
3419 : {
3420 114 : Location bloc = Linemap::predeclared_location();
3421 :
3422 : // The first four bytes are the length of the program in target byte
3423 : // order. Build a struct whose first type is uint32 to make this
3424 : // work.
3425 :
3426 114 : Type* uint32_type = Type::lookup_integer_type("uint32");
3427 :
3428 114 : Type* byte_type = Type::lookup_integer_type("byte");
3429 114 : Expression* len = Expression::make_integer_ul(this->bytes_.size(), NULL,
3430 : bloc);
3431 114 : Array_type* at = Type::make_array_type(byte_type, len);
3432 :
3433 114 : Struct_type* st = Type::make_builtin_struct_type(2, "len", uint32_type,
3434 : "bytes", at);
3435 :
3436 114 : Expression_list* vals = new Expression_list();
3437 114 : vals->reserve(this->bytes_.size());
3438 114 : for (std::vector<unsigned char>::const_iterator p = this->bytes_.begin();
3439 1245 : p != this->bytes_.end();
3440 1131 : ++p)
3441 1131 : vals->push_back(Expression::make_integer_ul(*p, byte_type, bloc));
3442 114 : Expression* bytes = Expression::make_array_composite_literal(at, vals, bloc);
3443 :
3444 114 : vals = new Expression_list();
3445 114 : vals->push_back(Expression::make_integer_ul(this->bytes_.size(), uint32_type,
3446 : bloc));
3447 114 : vals->push_back(bytes);
3448 :
3449 114 : return Expression::make_struct_composite_literal(st, vals, bloc);
3450 : }
3451 :
3452 : // Return a composite literal for the garbage collection program for
3453 : // this type. This is only used for types that are too large to use a
3454 : // ptrmask.
3455 :
3456 : Expression*
3457 114 : Type::gcprog_constructor(Gogo* gogo, int64_t ptrsize, int64_t ptrdata)
3458 : {
3459 114 : Location bloc = Linemap::predeclared_location();
3460 :
3461 114 : GCProg prog;
3462 114 : prog.set_from(gogo, this, ptrsize, 0);
3463 114 : int64_t offset = prog.bit_index() * ptrsize;
3464 114 : prog.end();
3465 :
3466 114 : int64_t type_size;
3467 114 : if (!this->backend_type_size(gogo, &type_size))
3468 : {
3469 0 : go_assert(saw_errors());
3470 0 : return Expression::make_error(bloc);
3471 : }
3472 :
3473 114 : go_assert(offset >= ptrdata && offset <= type_size);
3474 :
3475 114 : return prog.constructor();
3476 114 : }
3477 :
3478 : // Return a composite literal for the uncommon type information for
3479 : // this type. UNCOMMON_STRUCT_TYPE is the type of the uncommon type
3480 : // struct. If name is not NULL, it is the name of the type. If
3481 : // METHODS is not NULL, it is the list of methods. ONLY_VALUE_METHODS
3482 : // is true if only value methods should be included. At least one of
3483 : // NAME and METHODS must not be NULL.
3484 :
3485 : Expression*
3486 92464 : Type::uncommon_type_constructor(Gogo* gogo, Type* uncommon_type,
3487 : Named_type* name, const Methods* methods,
3488 : bool only_value_methods) const
3489 : {
3490 92464 : Location bloc = Linemap::predeclared_location();
3491 :
3492 184928 : const Struct_field_list* fields = uncommon_type->struct_type()->fields();
3493 :
3494 92464 : Expression_list* vals = new Expression_list();
3495 92464 : vals->reserve(3);
3496 :
3497 92464 : Struct_field_list::const_iterator p = fields->begin();
3498 92464 : go_assert(p->is_field_name("name"));
3499 :
3500 92464 : ++p;
3501 92464 : go_assert(p->is_field_name("pkgPath"));
3502 :
3503 92464 : if (name == NULL)
3504 : {
3505 29511 : vals->push_back(Expression::make_nil(bloc));
3506 29511 : vals->push_back(Expression::make_nil(bloc));
3507 : }
3508 : else
3509 : {
3510 62953 : Named_object* no = name->named_object();
3511 62953 : std::string n = Gogo::unpack_hidden_name(no->name());
3512 62953 : Expression* s = Expression::make_string(n, bloc);
3513 62953 : vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3514 :
3515 62953 : if (name->is_builtin())
3516 12343 : vals->push_back(Expression::make_nil(bloc));
3517 : else
3518 : {
3519 50610 : const Package* package = no->package();
3520 50610 : const std::string& pkgpath(package == NULL
3521 50610 : ? gogo->pkgpath()
3522 19843 : : package->pkgpath());
3523 50610 : s = Expression::make_string(pkgpath, bloc);
3524 50610 : vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3525 : }
3526 62953 : }
3527 :
3528 92464 : ++p;
3529 92464 : go_assert(p->is_field_name("methods"));
3530 92464 : vals->push_back(this->methods_constructor(gogo, p->type(), methods,
3531 : only_value_methods));
3532 :
3533 92464 : ++p;
3534 92464 : go_assert(p == fields->end());
3535 :
3536 92464 : Expression* r = Expression::make_struct_composite_literal(uncommon_type,
3537 : vals, bloc);
3538 92464 : return Expression::make_unary(OPERATOR_AND, r, bloc);
3539 : }
3540 :
3541 : // Sort methods by name.
3542 :
3543 : class Sort_methods
3544 : {
3545 : public:
3546 : bool
3547 1353730 : operator()(const std::pair<std::string, const Method*>& m1,
3548 : const std::pair<std::string, const Method*>& m2) const
3549 : {
3550 1353730 : return (Gogo::unpack_hidden_name(m1.first)
3551 2707460 : < Gogo::unpack_hidden_name(m2.first));
3552 : }
3553 : };
3554 :
3555 : // Return a composite literal for the type method table for this type.
3556 : // METHODS_TYPE is the type of the table, and is a slice type.
3557 : // METHODS is the list of methods. If ONLY_VALUE_METHODS is true,
3558 : // then only value methods are used.
3559 :
3560 : Expression*
3561 92464 : Type::methods_constructor(Gogo* gogo, Type* methods_type,
3562 : const Methods* methods,
3563 : bool only_value_methods) const
3564 : {
3565 92464 : Location bloc = Linemap::predeclared_location();
3566 :
3567 92464 : std::vector<std::pair<std::string, const Method*> > smethods;
3568 92464 : if (methods != NULL)
3569 : {
3570 56522 : smethods.reserve(methods->count());
3571 525486 : for (Methods::const_iterator p = methods->begin();
3572 525486 : p != methods->end();
3573 468964 : ++p)
3574 : {
3575 468964 : if (p->second->is_ambiguous())
3576 120 : continue;
3577 468844 : if (only_value_methods && !p->second->is_value_method())
3578 169212 : continue;
3579 :
3580 : // This is where we implement the magic //go:nointerface
3581 : // comment. If we saw that comment, we don't add this
3582 : // method to the type descriptor.
3583 299632 : if (p->second->nointerface())
3584 3 : continue;
3585 :
3586 599258 : smethods.push_back(std::make_pair(p->first, p->second));
3587 : }
3588 : }
3589 :
3590 92464 : if (smethods.empty())
3591 52923 : return Expression::make_slice_composite_literal(methods_type, NULL, bloc);
3592 :
3593 39541 : std::sort(smethods.begin(), smethods.end(), Sort_methods());
3594 :
3595 79082 : Type* method_type = methods_type->array_type()->element_type();
3596 :
3597 39541 : Expression_list* vals = new Expression_list();
3598 39541 : vals->reserve(smethods.size());
3599 39541 : for (std::vector<std::pair<std::string, const Method*> >::const_iterator p
3600 39541 : = smethods.begin();
3601 339170 : p != smethods.end();
3602 299629 : ++p)
3603 299629 : vals->push_back(this->method_constructor(gogo, method_type, p->first,
3604 299629 : p->second, only_value_methods));
3605 :
3606 39541 : return Expression::make_slice_composite_literal(methods_type, vals, bloc);
3607 92464 : }
3608 :
3609 : // Return a composite literal for a single method. METHOD_TYPE is the
3610 : // type of the entry. METHOD_NAME is the name of the method and M is
3611 : // the method information.
3612 :
3613 : Expression*
3614 299629 : Type::method_constructor(Gogo*, Type* method_type,
3615 : const std::string& method_name,
3616 : const Method* m,
3617 : bool only_value_methods) const
3618 : {
3619 299629 : Location bloc = Linemap::predeclared_location();
3620 :
3621 599258 : const Struct_field_list* fields = method_type->struct_type()->fields();
3622 :
3623 299629 : Expression_list* vals = new Expression_list();
3624 299629 : vals->reserve(5);
3625 :
3626 299629 : Struct_field_list::const_iterator p = fields->begin();
3627 299629 : go_assert(p->is_field_name("name"));
3628 299629 : const std::string n = Gogo::unpack_hidden_name(method_name);
3629 299629 : Expression* s = Expression::make_string(n, bloc);
3630 299629 : vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3631 :
3632 299629 : ++p;
3633 299629 : go_assert(p->is_field_name("pkgPath"));
3634 299629 : if (!Gogo::is_hidden_name(method_name))
3635 205747 : vals->push_back(Expression::make_nil(bloc));
3636 : else
3637 : {
3638 93882 : s = Expression::make_string(Gogo::hidden_name_pkgpath(method_name),
3639 : bloc);
3640 93882 : vals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
3641 : }
3642 :
3643 : // The direct_iface_stub dereferences the value stored in the
3644 : // interface when calling the method.
3645 : //
3646 : // We need this for a value method if this type is a pointer to a
3647 : // direct-iface type. For example, if we have "type C chan int" and M
3648 : // is a value method on C, then since a channel is a direct-iface type
3649 : // M expects a value of type C. We are generating the method table
3650 : // for *C, so the value stored in the interface is *C. We have to
3651 : // call the direct-iface stub to dereference *C to get C to pass to M.
3652 : //
3653 : // We also need this for a pointer method if the pointer itself is not
3654 : // a direct-iface type, as arises for notinheap types. In this case
3655 : // we have "type NIH ..." where NIH is go:notinheap. Since NIH is
3656 : // notinheap, *NIH is a pointer type that is not a direct-iface type,
3657 : // so the value stored in the interface is actually **NIH. The method
3658 : // expects *NIH, so we have to call the direct-iface stub to
3659 : // dereference **NIH to get *NIH to pass to M. (This case doesn't
3660 : // arise for value methods because pointer types can't have methods,
3661 : // so there is no such thing as a value method for type *NIH.)
3662 :
3663 299629 : bool use_direct_iface_stub = false;
3664 299629 : if (m->is_value_method()
3665 79922 : && this->points_to() != NULL
3666 326286 : && this->points_to()->is_direct_iface_type())
3667 : use_direct_iface_stub = true;
3668 299629 : if (!m->is_value_method()
3669 219707 : && this->points_to() != NULL
3670 519336 : && !this->is_direct_iface_type())
3671 : use_direct_iface_stub = true;
3672 :
3673 298951 : Named_object* no = (use_direct_iface_stub
3674 299629 : ? m->iface_stub_object()
3675 296414 : : (m->needs_stub_method()
3676 296414 : ? m->stub_object()
3677 254141 : : m->named_object()));
3678 :
3679 299629 : Function_type* mtype;
3680 299629 : if (no->is_function())
3681 79998 : mtype = no->func_value()->type();
3682 : else
3683 219631 : mtype = no->func_declaration_value()->type();
3684 299629 : go_assert(mtype->is_method());
3685 299629 : Type* nonmethod_type = mtype->copy_without_receiver();
3686 :
3687 299629 : ++p;
3688 299629 : go_assert(p->is_field_name("mtyp"));
3689 299629 : vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3690 :
3691 299629 : ++p;
3692 299629 : go_assert(p->is_field_name("typ"));
3693 246364 : bool want_pointer_receiver = (!only_value_methods && m->is_value_method()
3694 326286 : && !use_direct_iface_stub);
3695 299629 : nonmethod_type = mtype->copy_with_receiver_as_param(want_pointer_receiver);
3696 299629 : vals->push_back(Expression::make_type_descriptor(nonmethod_type, bloc));
3697 :
3698 299629 : ++p;
3699 299629 : go_assert(p->is_field_name("tfn"));
3700 299629 : vals->push_back(Expression::make_func_code_reference(no, bloc));
3701 :
3702 299629 : ++p;
3703 299629 : go_assert(p == fields->end());
3704 :
3705 299629 : return Expression::make_struct_composite_literal(method_type, vals, bloc);
3706 299629 : }
3707 :
3708 : // Return a composite literal for the type descriptor of a plain type.
3709 : // RUNTIME_TYPE_KIND is the value of the kind field. If NAME is not
3710 : // NULL, it is the name to use as well as the list of methods.
3711 :
3712 : Expression*
3713 18363 : Type::plain_type_descriptor(Gogo* gogo, int runtime_type_kind,
3714 : Named_type* name)
3715 : {
3716 18363 : return this->type_descriptor_constructor(gogo, runtime_type_kind,
3717 18363 : name, NULL, true);
3718 : }
3719 :
3720 : // Return the type reflection string for this type.
3721 :
3722 : std::string
3723 283752 : Type::reflection(Gogo* gogo) const
3724 : {
3725 283752 : std::string ret;
3726 :
3727 : // The do_reflection virtual function should set RET to the
3728 : // reflection string.
3729 283752 : this->do_reflection(gogo, &ret);
3730 :
3731 283752 : return ret;
3732 : }
3733 :
3734 : // Return whether the backend size of the type is known.
3735 :
3736 : bool
3737 21102529 : Type::is_backend_type_size_known(Gogo* gogo)
3738 : {
3739 23764481 : switch (this->classification_)
3740 : {
3741 : case TYPE_ERROR:
3742 : case TYPE_VOID:
3743 : case TYPE_BOOLEAN:
3744 : case TYPE_INTEGER:
3745 : case TYPE_FLOAT:
3746 : case TYPE_COMPLEX:
3747 : case TYPE_STRING:
3748 : case TYPE_FUNCTION:
3749 : case TYPE_POINTER:
3750 : case TYPE_NIL:
3751 : case TYPE_MAP:
3752 : case TYPE_CHANNEL:
3753 : case TYPE_INTERFACE:
3754 : return true;
3755 :
3756 1208174 : case TYPE_STRUCT:
3757 1208174 : {
3758 2416348 : const Struct_field_list* fields = this->struct_type()->fields();
3759 4887588 : for (Struct_field_list::const_iterator pf = fields->begin();
3760 4887588 : pf != fields->end();
3761 3679414 : ++pf)
3762 3679414 : if (!pf->type()->is_backend_type_size_known(gogo))
3763 0 : return false;
3764 : return true;
3765 : }
3766 :
3767 1911247 : case TYPE_ARRAY:
3768 1911247 : {
3769 1911247 : const Array_type* at = this->array_type();
3770 1911247 : if (at->length() == NULL)
3771 : return true;
3772 : else
3773 : {
3774 1469662 : Numeric_constant nc;
3775 1469662 : if (!at->length()->numeric_constant_value(&nc))
3776 : return false;
3777 1469662 : mpz_t ival;
3778 1469662 : if (!nc.to_int(&ival))
3779 : return false;
3780 1469662 : mpz_clear(ival);
3781 1469662 : return at->element_type()->is_backend_type_size_known(gogo);
3782 1469662 : }
3783 : }
3784 :
3785 12585567 : case TYPE_NAMED:
3786 12585567 : this->named_type()->convert(gogo);
3787 12585567 : return this->named_type()->is_named_backend_type_size_known();
3788 :
3789 2661952 : case TYPE_FORWARD:
3790 2661952 : {
3791 2661952 : Forward_declaration_type* fdt = this->forward_declaration_type();
3792 2661952 : return fdt->real_type()->is_backend_type_size_known(gogo);
3793 : }
3794 :
3795 0 : case TYPE_SINK:
3796 0 : case TYPE_CALL_MULTIPLE_RESULT:
3797 0 : go_unreachable();
3798 :
3799 0 : default:
3800 0 : go_unreachable();
3801 : }
3802 : }
3803 :
3804 : // If the size of the type can be determined, set *PSIZE to the size
3805 : // in bytes and return true. Otherwise, return false. This queries
3806 : // the backend.
3807 :
3808 : bool
3809 8974828 : Type::backend_type_size(Gogo* gogo, int64_t *psize)
3810 : {
3811 8974828 : if (!this->is_backend_type_size_known(gogo))
3812 : return false;
3813 8974828 : if (this->is_error_type())
3814 : return false;
3815 8974825 : Btype* bt = this->get_backend_placeholder(gogo);
3816 8974825 : *psize = gogo->backend()->type_size(bt);
3817 8974825 : if (*psize == -1)
3818 : {
3819 0 : if (this->named_type() != NULL)
3820 0 : go_error_at(this->named_type()->location(),
3821 : "type %s larger than address space",
3822 0 : Gogo::message_name(this->named_type()->name()).c_str());
3823 : else
3824 0 : go_error_at(Linemap::unknown_location(),
3825 : "type %s larger than address space",
3826 0 : this->reflection(gogo).c_str());
3827 :
3828 : // Make this an error type to avoid knock-on errors.
3829 0 : this->classification_ = TYPE_ERROR;
3830 0 : return false;
3831 : }
3832 : return true;
3833 : }
3834 :
3835 : // If the alignment of the type can be determined, set *PALIGN to
3836 : // the alignment in bytes and return true. Otherwise, return false.
3837 :
3838 : bool
3839 4552603 : Type::backend_type_align(Gogo* gogo, int64_t *palign)
3840 : {
3841 4552603 : if (!this->is_backend_type_size_known(gogo))
3842 : return false;
3843 4552603 : Btype* bt = this->get_backend_placeholder(gogo);
3844 4552603 : *palign = gogo->backend()->type_alignment(bt);
3845 4552603 : return true;
3846 : }
3847 :
3848 : // Like backend_type_align, but return the alignment when used as a
3849 : // field.
3850 :
3851 : bool
3852 1187050 : Type::backend_type_field_align(Gogo* gogo, int64_t *palign)
3853 : {
3854 1187050 : if (!this->is_backend_type_size_known(gogo))
3855 : return false;
3856 1187050 : Btype* bt = this->get_backend_placeholder(gogo);
3857 1187050 : *palign = gogo->backend()->type_field_alignment(bt);
3858 1187050 : return true;
3859 : }
3860 :
3861 : // Get the ptrdata value for a type. This is the size of the prefix
3862 : // of the type that contains all pointers. Store the ptrdata in
3863 : // *PPTRDATA and return whether we found it.
3864 :
3865 : bool
3866 1129904 : Type::backend_type_ptrdata(Gogo* gogo, int64_t* pptrdata)
3867 : {
3868 1238382 : *pptrdata = 0;
3869 :
3870 1238382 : if (!this->has_pointer())
3871 : return true;
3872 :
3873 1238361 : if (!this->is_backend_type_size_known(gogo))
3874 : return false;
3875 :
3876 1238361 : switch (this->classification_)
3877 : {
3878 : case TYPE_ERROR:
3879 : return true;
3880 :
3881 746071 : case TYPE_FUNCTION:
3882 746071 : case TYPE_POINTER:
3883 746071 : case TYPE_MAP:
3884 746071 : case TYPE_CHANNEL:
3885 : // These types are nothing but a pointer.
3886 746071 : return this->backend_type_size(gogo, pptrdata);
3887 :
3888 56778 : case TYPE_INTERFACE:
3889 : // An interface is a struct of two pointers.
3890 56778 : return this->backend_type_size(gogo, pptrdata);
3891 :
3892 44940 : case TYPE_STRING:
3893 44940 : {
3894 : // A string is a struct whose first field is a pointer, and
3895 : // whose second field is not.
3896 44940 : Type* uint8_type = Type::lookup_integer_type("uint8");
3897 44940 : Type* ptr = Type::make_pointer_type(uint8_type);
3898 44940 : return ptr->backend_type_size(gogo, pptrdata);
3899 : }
3900 :
3901 108478 : case TYPE_NAMED:
3902 108478 : case TYPE_FORWARD:
3903 108478 : return this->base()->backend_type_ptrdata(gogo, pptrdata);
3904 :
3905 136482 : case TYPE_STRUCT:
3906 136482 : {
3907 272964 : const Struct_field_list* fields = this->struct_type()->fields();
3908 136482 : int64_t offset = 0;
3909 136482 : const Struct_field *ptr = NULL;
3910 136482 : int64_t ptr_offset = 0;
3911 136482 : for (Struct_field_list::const_iterator pf = fields->begin();
3912 673834 : pf != fields->end();
3913 537352 : ++pf)
3914 : {
3915 537352 : int64_t field_align;
3916 537352 : if (!pf->type()->backend_type_field_align(gogo, &field_align))
3917 0 : return false;
3918 537352 : offset = (offset + (field_align - 1)) &~ (field_align - 1);
3919 :
3920 537352 : if (pf->type()->has_pointer())
3921 : {
3922 363036 : ptr = &*pf;
3923 363036 : ptr_offset = offset;
3924 : }
3925 :
3926 537352 : int64_t field_size;
3927 537352 : if (!pf->type()->backend_type_size(gogo, &field_size))
3928 : return false;
3929 537352 : offset += field_size;
3930 : }
3931 :
3932 136482 : if (ptr != NULL)
3933 : {
3934 136482 : int64_t ptr_ptrdata;
3935 136482 : if (!ptr->type()->backend_type_ptrdata(gogo, &ptr_ptrdata))
3936 0 : return false;
3937 136482 : *pptrdata = ptr_offset + ptr_ptrdata;
3938 : }
3939 : return true;
3940 : }
3941 :
3942 145604 : case TYPE_ARRAY:
3943 145604 : if (this->is_slice_type())
3944 : {
3945 : // A slice is a struct whose first field is a pointer, and
3946 : // whose remaining fields are not.
3947 194138 : Type* element_type = this->array_type()->element_type();
3948 97069 : Type* ptr = Type::make_pointer_type(element_type);
3949 97069 : return ptr->backend_type_size(gogo, pptrdata);
3950 : }
3951 : else
3952 : {
3953 48535 : Numeric_constant nc;
3954 97070 : if (!this->array_type()->length()->numeric_constant_value(&nc))
3955 : return false;
3956 48535 : int64_t len;
3957 48535 : if (!nc.to_memory_size(&len))
3958 : return false;
3959 :
3960 97070 : Type* element_type = this->array_type()->element_type();
3961 48535 : int64_t ele_size;
3962 48535 : int64_t ele_ptrdata;
3963 48535 : if (!element_type->backend_type_size(gogo, &ele_size)
3964 48535 : || !element_type->backend_type_ptrdata(gogo, &ele_ptrdata))
3965 0 : return false;
3966 48535 : go_assert(ele_size > 0 && ele_ptrdata > 0);
3967 :
3968 48535 : *pptrdata = (len - 1) * ele_size + ele_ptrdata;
3969 48535 : return true;
3970 48535 : }
3971 :
3972 0 : default:
3973 0 : case TYPE_VOID:
3974 0 : case TYPE_BOOLEAN:
3975 0 : case TYPE_INTEGER:
3976 0 : case TYPE_FLOAT:
3977 0 : case TYPE_COMPLEX:
3978 0 : case TYPE_SINK:
3979 0 : case TYPE_NIL:
3980 0 : case TYPE_CALL_MULTIPLE_RESULT:
3981 0 : go_unreachable();
3982 : }
3983 : }
3984 :
3985 : // Get the ptrdata value to store in a type descriptor. This is
3986 : // normally the same as backend_type_ptrdata, but for a type that is
3987 : // large enough to use a gcprog we may need to store a different value
3988 : // if it ends with an array. If the gcprog uses a repeat descriptor
3989 : // for the array, and if the array element ends with non-pointer data,
3990 : // then the gcprog will produce a value that describes the complete
3991 : // array where the backend ptrdata will omit the non-pointer elements
3992 : // of the final array element. This is a subtle difference but the
3993 : // run time code checks it to verify that it has expanded a gcprog as
3994 : // expected.
3995 :
3996 : bool
3997 212643 : Type::descriptor_ptrdata(Gogo* gogo, int64_t* pptrdata)
3998 : {
3999 212643 : int64_t backend_ptrdata;
4000 212643 : if (!this->backend_type_ptrdata(gogo, &backend_ptrdata))
4001 : return false;
4002 :
4003 212643 : int64_t ptrsize;
4004 212643 : if (!this->needs_gcprog(gogo, &ptrsize, &backend_ptrdata))
4005 : {
4006 212561 : *pptrdata = backend_ptrdata;
4007 212561 : return true;
4008 : }
4009 :
4010 82 : GCProg prog;
4011 82 : prog.set_from(gogo, this, ptrsize, 0);
4012 82 : int64_t offset = prog.bit_index() * ptrsize;
4013 :
4014 82 : go_assert(offset >= backend_ptrdata);
4015 82 : *pptrdata = offset;
4016 82 : return true;
4017 82 : }
4018 :
4019 : // Default function to export a type.
4020 :
4021 : void
4022 0 : Type::do_export(Export*) const
4023 : {
4024 0 : go_unreachable();
4025 : }
4026 :
4027 : // Import a type.
4028 :
4029 : Type*
4030 1312570 : Type::import_type(Import* imp)
4031 : {
4032 1312570 : if (imp->match_c_string("("))
4033 68687 : return Function_type::do_import(imp);
4034 1243883 : else if (imp->match_c_string("*"))
4035 495950 : return Pointer_type::do_import(imp);
4036 747933 : else if (imp->match_c_string("struct "))
4037 383011 : return Struct_type::do_import(imp);
4038 364922 : else if (imp->match_c_string("["))
4039 269509 : return Array_type::do_import(imp);
4040 95413 : else if (imp->match_c_string("map "))
4041 21174 : return Map_type::do_import(imp);
4042 74239 : else if (imp->match_c_string("chan "))
4043 9122 : return Channel_type::do_import(imp);
4044 65117 : else if (imp->match_c_string("interface"))
4045 65117 : return Interface_type::do_import(imp);
4046 : else
4047 : {
4048 0 : go_error_at(imp->location(), "import error: expected type");
4049 0 : return Type::make_error_type();
4050 : }
4051 : }
4052 :
4053 : // Class Error_type.
4054 :
4055 : // Return the backend representation of an Error type.
4056 :
4057 : Btype*
4058 0 : Error_type::do_get_backend(Gogo* gogo)
4059 : {
4060 0 : return gogo->backend()->error_type();
4061 : }
4062 :
4063 : // Return an expression for the type descriptor for an error type.
4064 :
4065 :
4066 : Expression*
4067 1 : Error_type::do_type_descriptor(Gogo*, Named_type*)
4068 : {
4069 1 : return Expression::make_error(Linemap::predeclared_location());
4070 : }
4071 :
4072 : // We should not be asked for the reflection string for an error type.
4073 :
4074 : void
4075 1 : Error_type::do_reflection(Gogo*, std::string*) const
4076 : {
4077 1 : go_assert(saw_errors());
4078 1 : }
4079 :
4080 : Type*
4081 15710 : Type::make_error_type()
4082 : {
4083 15710 : static Error_type singleton_error_type;
4084 15710 : return &singleton_error_type;
4085 : }
4086 :
4087 : // Class Void_type.
4088 :
4089 : // Get the backend representation of a void type.
4090 :
4091 : Btype*
4092 4646 : Void_type::do_get_backend(Gogo* gogo)
4093 : {
4094 4646 : return gogo->backend()->void_type();
4095 : }
4096 :
4097 : Type*
4098 6726559 : Type::make_void_type()
4099 : {
4100 6726559 : static Void_type singleton_void_type;
4101 6726559 : return &singleton_void_type;
4102 : }
4103 :
4104 : // Class Boolean_type.
4105 :
4106 : // Return the backend representation of the boolean type.
4107 :
4108 : Btype*
4109 6768 : Boolean_type::do_get_backend(Gogo* gogo)
4110 : {
4111 6768 : return gogo->backend()->bool_type();
4112 : }
4113 :
4114 : // Make the type descriptor.
4115 :
4116 : Expression*
4117 1335 : Boolean_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4118 : {
4119 1335 : if (name != NULL)
4120 1335 : return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_BOOL, name);
4121 : else
4122 : {
4123 0 : Named_object* no = gogo->lookup_global("bool");
4124 0 : go_assert(no != NULL);
4125 0 : return Type::type_descriptor(gogo, no->type_value());
4126 : }
4127 : }
4128 :
4129 : Type*
4130 851747 : Type::make_boolean_type()
4131 : {
4132 851747 : static Boolean_type boolean_type;
4133 851747 : return &boolean_type;
4134 : }
4135 :
4136 : // The named type "bool".
4137 :
4138 : static Named_type* named_bool_type;
4139 :
4140 : // Get the named type "bool".
4141 :
4142 : Named_type*
4143 2491004 : Type::lookup_bool_type()
4144 : {
4145 2491004 : return named_bool_type;
4146 : }
4147 :
4148 : // Make the named type "bool".
4149 :
4150 : Named_type*
4151 4646 : Type::make_named_bool_type()
4152 : {
4153 4646 : Type* bool_type = Type::make_boolean_type();
4154 4646 : Named_object* named_object =
4155 4646 : Named_object::make_type("bool", NULL, bool_type,
4156 : Linemap::predeclared_location());
4157 4646 : Named_type* named_type = named_object->type_value();
4158 4646 : named_bool_type = named_type;
4159 4646 : return named_type;
4160 : }
4161 :
4162 : // Class Integer_type.
4163 :
4164 : Integer_type::Named_integer_types Integer_type::named_integer_types;
4165 :
4166 : // Create a new integer type. Non-abstract integer types always have
4167 : // names.
4168 :
4169 : Named_type*
4170 51106 : Integer_type::create_integer_type(const char* name, bool is_unsigned,
4171 : int bits, int runtime_type_kind)
4172 : {
4173 51106 : Integer_type* integer_type = new Integer_type(false, is_unsigned, bits,
4174 51106 : runtime_type_kind);
4175 51106 : std::string sname(name);
4176 51106 : Named_object* named_object =
4177 51106 : Named_object::make_type(sname, NULL, integer_type,
4178 : Linemap::predeclared_location());
4179 51106 : Named_type* named_type = named_object->type_value();
4180 51106 : std::pair<Named_integer_types::iterator, bool> ins =
4181 51106 : Integer_type::named_integer_types.insert(std::make_pair(sname, named_type));
4182 51106 : go_assert(ins.second);
4183 51106 : return named_type;
4184 51106 : }
4185 :
4186 : // Look up an existing integer type.
4187 :
4188 : Named_type*
4189 17983929 : Integer_type::lookup_integer_type(const char* name)
4190 : {
4191 17983929 : Named_integer_types::const_iterator p =
4192 17983929 : Integer_type::named_integer_types.find(name);
4193 17983929 : go_assert(p != Integer_type::named_integer_types.end());
4194 17983929 : return p->second;
4195 : }
4196 :
4197 : // Create a new abstract integer type.
4198 :
4199 : Integer_type*
4200 1152510 : Integer_type::create_abstract_integer_type()
4201 : {
4202 1152510 : static Integer_type* abstract_type;
4203 1152510 : if (abstract_type == NULL)
4204 : {
4205 4646 : Type* int_type = Type::lookup_integer_type("int");
4206 9292 : abstract_type = new Integer_type(true, false,
4207 : int_type->integer_type()->bits(),
4208 9292 : RUNTIME_TYPE_KIND_INT);
4209 : }
4210 1152510 : return abstract_type;
4211 : }
4212 :
4213 : // Create a new abstract character type.
4214 :
4215 : Integer_type*
4216 22448 : Integer_type::create_abstract_character_type()
4217 : {
4218 22448 : static Integer_type* abstract_type;
4219 22448 : if (abstract_type == NULL)
4220 : {
4221 3422 : abstract_type = new Integer_type(true, false, 32,
4222 1711 : RUNTIME_TYPE_KIND_INT32);
4223 1711 : abstract_type->set_is_rune();
4224 : }
4225 22448 : return abstract_type;
4226 : }
4227 :
4228 : // Create an alias to an integer type. This is used for byte and rune.
4229 :
4230 : Named_type*
4231 9292 : Integer_type::create_integer_type_alias(const char* name,
4232 : Named_type* real_type)
4233 : {
4234 9292 : std::string sname(name);
4235 9292 : Named_object* no = Named_object::make_type(sname, NULL, real_type,
4236 : Linemap::predeclared_location());
4237 9292 : Named_type* nt = no->type_value();
4238 9292 : nt->set_is_alias();
4239 9292 : std::pair<Named_integer_types::iterator, bool> ins =
4240 9292 : Integer_type::named_integer_types.insert(std::make_pair(sname, nt));
4241 9292 : go_assert(ins.second);
4242 9292 : return nt;
4243 9292 : }
4244 :
4245 : // Integer type compatibility.
4246 :
4247 : bool
4248 287752 : Integer_type::is_identical(const Integer_type* t) const
4249 : {
4250 287752 : if (this->is_unsigned_ != t->is_unsigned_ || this->bits_ != t->bits_)
4251 : return false;
4252 52754 : return this->is_abstract_ == t->is_abstract_;
4253 : }
4254 :
4255 : // Message name.
4256 :
4257 : void
4258 8 : Integer_type::do_message_name(std::string* ret) const
4259 : {
4260 8 : ret->append("<untyped ");
4261 8 : if (this->is_byte_)
4262 0 : ret->append("byte");
4263 8 : else if (this->is_rune_)
4264 4 : ret->append("rune");
4265 : else
4266 : {
4267 4 : if (this->is_unsigned_)
4268 0 : ret->push_back('u');
4269 4 : if (this->is_abstract_)
4270 4 : ret->append("int");
4271 : else
4272 : {
4273 0 : ret->append("int");
4274 0 : char buf[10];
4275 0 : snprintf(buf, sizeof buf, "%d", this->bits_);
4276 0 : ret->append(buf);
4277 : }
4278 : }
4279 8 : ret->push_back('>');
4280 8 : }
4281 :
4282 : // Hash code.
4283 :
4284 : unsigned int
4285 54122 : Integer_type::do_hash_for_method(Gogo*, int) const
4286 : {
4287 54122 : return ((this->bits_ << 4)
4288 54122 : + ((this->is_unsigned_ ? 1 : 0) << 8)
4289 54122 : + ((this->is_abstract_ ? 1 : 0) << 9));
4290 : }
4291 :
4292 : // Convert an Integer_type to the backend representation.
4293 :
4294 : Btype*
4295 178073 : Integer_type::do_get_backend(Gogo* gogo)
4296 : {
4297 178073 : if (this->is_abstract_)
4298 : {
4299 0 : go_assert(saw_errors());
4300 0 : return gogo->backend()->error_type();
4301 : }
4302 178073 : return gogo->backend()->integer_type(this->is_unsigned_, this->bits_);
4303 : }
4304 :
4305 : // The type descriptor for an integer type. Integer types are always
4306 : // named.
4307 :
4308 : Expression*
4309 13352 : Integer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4310 : {
4311 13352 : go_assert(name != NULL || saw_errors());
4312 13352 : return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4313 : }
4314 :
4315 : // We should not be asked for the reflection string of a basic type.
4316 :
4317 : void
4318 0 : Integer_type::do_reflection(Gogo*, std::string*) const
4319 : {
4320 0 : go_assert(saw_errors());
4321 0 : }
4322 :
4323 : // Make an integer type.
4324 :
4325 : Named_type*
4326 51106 : Type::make_integer_type(const char* name, bool is_unsigned, int bits,
4327 : int runtime_type_kind)
4328 : {
4329 51106 : return Integer_type::create_integer_type(name, is_unsigned, bits,
4330 51106 : runtime_type_kind);
4331 : }
4332 :
4333 : // Make an abstract integer type.
4334 :
4335 : Integer_type*
4336 1152510 : Type::make_abstract_integer_type()
4337 : {
4338 1152510 : return Integer_type::create_abstract_integer_type();
4339 : }
4340 :
4341 : // Make an abstract character type.
4342 :
4343 : Integer_type*
4344 22448 : Type::make_abstract_character_type()
4345 : {
4346 22448 : return Integer_type::create_abstract_character_type();
4347 : }
4348 :
4349 : // Make an integer type alias.
4350 :
4351 : Named_type*
4352 9292 : Type::make_integer_type_alias(const char* name, Named_type* real_type)
4353 : {
4354 9292 : return Integer_type::create_integer_type_alias(name, real_type);
4355 : }
4356 :
4357 : // Look up an integer type.
4358 :
4359 : Named_type*
4360 17983929 : Type::lookup_integer_type(const char* name)
4361 : {
4362 17983929 : return Integer_type::lookup_integer_type(name);
4363 : }
4364 :
4365 : // Class Float_type.
4366 :
4367 : Float_type::Named_float_types Float_type::named_float_types;
4368 :
4369 : // Create a new float type. Non-abstract float types always have
4370 : // names.
4371 :
4372 : Named_type*
4373 9292 : Float_type::create_float_type(const char* name, int bits,
4374 : int runtime_type_kind)
4375 : {
4376 9292 : Float_type* float_type = new Float_type(false, bits, runtime_type_kind);
4377 9292 : std::string sname(name);
4378 9292 : Named_object* named_object =
4379 9292 : Named_object::make_type(sname, NULL, float_type,
4380 : Linemap::predeclared_location());
4381 9292 : Named_type* named_type = named_object->type_value();
4382 9292 : std::pair<Named_float_types::iterator, bool> ins =
4383 9292 : Float_type::named_float_types.insert(std::make_pair(sname, named_type));
4384 9292 : go_assert(ins.second);
4385 9292 : return named_type;
4386 9292 : }
4387 :
4388 : // Look up an existing float type.
4389 :
4390 : Named_type*
4391 23637 : Float_type::lookup_float_type(const char* name)
4392 : {
4393 23637 : Named_float_types::const_iterator p =
4394 23637 : Float_type::named_float_types.find(name);
4395 23637 : go_assert(p != Float_type::named_float_types.end());
4396 23637 : return p->second;
4397 : }
4398 :
4399 : // Create a new abstract float type.
4400 :
4401 : Float_type*
4402 16013 : Float_type::create_abstract_float_type()
4403 : {
4404 16013 : static Float_type* abstract_type;
4405 16013 : if (abstract_type == NULL)
4406 479 : abstract_type = new Float_type(true, 64, RUNTIME_TYPE_KIND_FLOAT64);
4407 16013 : return abstract_type;
4408 : }
4409 :
4410 : // Whether this type is identical with T.
4411 :
4412 : bool
4413 1358 : Float_type::is_identical(const Float_type* t) const
4414 : {
4415 1358 : if (this->bits_ != t->bits_)
4416 : return false;
4417 46 : return this->is_abstract_ == t->is_abstract_;
4418 : }
4419 :
4420 : // Message name.
4421 :
4422 : void
4423 0 : Float_type::do_message_name(std::string* ret) const
4424 : {
4425 0 : ret->append("<untyped float");
4426 0 : if (!this->is_abstract_)
4427 : {
4428 0 : char buf[10];
4429 0 : snprintf(buf, sizeof buf, "%d", this->bits_);
4430 0 : ret->append(buf);
4431 : }
4432 0 : ret->push_back('>');
4433 0 : }
4434 :
4435 : // Hash code.
4436 :
4437 : unsigned int
4438 8942 : Float_type::do_hash_for_method(Gogo*, int) const
4439 : {
4440 8942 : return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4441 : }
4442 :
4443 : // Convert to the backend representation.
4444 :
4445 : Btype*
4446 9827 : Float_type::do_get_backend(Gogo* gogo)
4447 : {
4448 9827 : return gogo->backend()->float_type(this->bits_);
4449 : }
4450 :
4451 : // The type descriptor for a float type. Float types are always named.
4452 :
4453 : Expression*
4454 489 : Float_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4455 : {
4456 489 : go_assert(name != NULL || saw_errors());
4457 489 : return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4458 : }
4459 :
4460 : // We should not be asked for the reflection string of a basic type.
4461 :
4462 : void
4463 0 : Float_type::do_reflection(Gogo*, std::string*) const
4464 : {
4465 0 : go_assert(saw_errors());
4466 0 : }
4467 :
4468 : // Make a floating point type.
4469 :
4470 : Named_type*
4471 9292 : Type::make_float_type(const char* name, int bits, int runtime_type_kind)
4472 : {
4473 9292 : return Float_type::create_float_type(name, bits, runtime_type_kind);
4474 : }
4475 :
4476 : // Make an abstract float type.
4477 :
4478 : Float_type*
4479 16013 : Type::make_abstract_float_type()
4480 : {
4481 16013 : return Float_type::create_abstract_float_type();
4482 : }
4483 :
4484 : // Look up a float type.
4485 :
4486 : Named_type*
4487 23637 : Type::lookup_float_type(const char* name)
4488 : {
4489 23637 : return Float_type::lookup_float_type(name);
4490 : }
4491 :
4492 : // Class Complex_type.
4493 :
4494 : Complex_type::Named_complex_types Complex_type::named_complex_types;
4495 :
4496 : // Create a new complex type. Non-abstract complex types always have
4497 : // names.
4498 :
4499 : Named_type*
4500 9292 : Complex_type::create_complex_type(const char* name, int bits,
4501 : int runtime_type_kind)
4502 : {
4503 9292 : Complex_type* complex_type = new Complex_type(false, bits,
4504 9292 : runtime_type_kind);
4505 9292 : std::string sname(name);
4506 9292 : Named_object* named_object =
4507 9292 : Named_object::make_type(sname, NULL, complex_type,
4508 : Linemap::predeclared_location());
4509 9292 : Named_type* named_type = named_object->type_value();
4510 9292 : std::pair<Named_complex_types::iterator, bool> ins =
4511 9292 : Complex_type::named_complex_types.insert(std::make_pair(sname,
4512 : named_type));
4513 9292 : go_assert(ins.second);
4514 9292 : return named_type;
4515 9292 : }
4516 :
4517 : // Look up an existing complex type.
4518 :
4519 : Named_type*
4520 89237 : Complex_type::lookup_complex_type(const char* name)
4521 : {
4522 89237 : Named_complex_types::const_iterator p =
4523 89237 : Complex_type::named_complex_types.find(name);
4524 89237 : go_assert(p != Complex_type::named_complex_types.end());
4525 89237 : return p->second;
4526 : }
4527 :
4528 : // Create a new abstract complex type.
4529 :
4530 : Complex_type*
4531 2444 : Complex_type::create_abstract_complex_type()
4532 : {
4533 2444 : static Complex_type* abstract_type;
4534 2444 : if (abstract_type == NULL)
4535 77 : abstract_type = new Complex_type(true, 128, RUNTIME_TYPE_KIND_COMPLEX128);
4536 2444 : return abstract_type;
4537 : }
4538 :
4539 : // Whether this type is identical with T.
4540 :
4541 : bool
4542 162 : Complex_type::is_identical(const Complex_type *t) const
4543 : {
4544 162 : if (this->bits_ != t->bits_)
4545 : return false;
4546 49 : return this->is_abstract_ == t->is_abstract_;
4547 : }
4548 :
4549 : // Message name.
4550 :
4551 : void
4552 3 : Complex_type::do_message_name(std::string* ret) const
4553 : {
4554 3 : ret->append("<untyped complex");
4555 3 : if (!this->is_abstract_)
4556 : {
4557 0 : char buf[10];
4558 0 : snprintf(buf, sizeof buf, "%d", this->bits_);
4559 0 : ret->append(buf);
4560 : }
4561 3 : ret->push_back('>');
4562 3 : }
4563 :
4564 : // Hash code.
4565 :
4566 : unsigned int
4567 8654 : Complex_type::do_hash_for_method(Gogo*, int) const
4568 : {
4569 8654 : return (this->bits_ << 4) + ((this->is_abstract_ ? 1 : 0) << 8);
4570 : }
4571 :
4572 : // Convert to the backend representation.
4573 :
4574 : Btype*
4575 9466 : Complex_type::do_get_backend(Gogo* gogo)
4576 : {
4577 9466 : return gogo->backend()->complex_type(this->bits_);
4578 : }
4579 :
4580 : // The type descriptor for a complex type. Complex types are always
4581 : // named.
4582 :
4583 : Expression*
4584 160 : Complex_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4585 : {
4586 160 : go_assert(name != NULL || saw_errors());
4587 160 : return this->plain_type_descriptor(gogo, this->runtime_type_kind_, name);
4588 : }
4589 :
4590 : // We should not be asked for the reflection string of a basic type.
4591 :
4592 : void
4593 0 : Complex_type::do_reflection(Gogo*, std::string*) const
4594 : {
4595 0 : go_assert(saw_errors());
4596 0 : }
4597 :
4598 : // Make a complex type.
4599 :
4600 : Named_type*
4601 9292 : Type::make_complex_type(const char* name, int bits, int runtime_type_kind)
4602 : {
4603 9292 : return Complex_type::create_complex_type(name, bits, runtime_type_kind);
4604 : }
4605 :
4606 : // Make an abstract complex type.
4607 :
4608 : Complex_type*
4609 2444 : Type::make_abstract_complex_type()
4610 : {
4611 2444 : return Complex_type::create_abstract_complex_type();
4612 : }
4613 :
4614 : // Look up a complex type.
4615 :
4616 : Named_type*
4617 89237 : Type::lookup_complex_type(const char* name)
4618 : {
4619 89237 : return Complex_type::lookup_complex_type(name);
4620 : }
4621 :
4622 : // Class String_type.
4623 :
4624 : // Convert String_type to the backend representation. A string is a
4625 : // struct with two fields: a pointer to the characters and a length.
4626 :
4627 : Btype*
4628 11311 : String_type::do_get_backend(Gogo* gogo)
4629 : {
4630 11311 : static Btype* backend_string_type;
4631 11311 : if (backend_string_type == NULL)
4632 : {
4633 4646 : std::vector<Backend::Btyped_identifier> fields(2);
4634 :
4635 4646 : Type* b = Type::lookup_integer_type("byte");
4636 4646 : Type* pb = Type::make_pointer_type(b);
4637 :
4638 : // We aren't going to get back to this field to finish the
4639 : // backend representation, so force it to be finished now.
4640 4646 : if (!gogo->named_types_are_converted())
4641 : {
4642 4646 : Btype* bt = pb->get_backend_placeholder(gogo);
4643 4646 : pb->finish_backend(gogo, bt);
4644 : }
4645 :
4646 4646 : fields[0].name = "__data";
4647 4646 : fields[0].btype = pb->get_backend(gogo);
4648 4646 : fields[0].location = Linemap::predeclared_location();
4649 :
4650 4646 : Type* int_type = Type::lookup_integer_type("int");
4651 4646 : fields[1].name = "__length";
4652 4646 : fields[1].btype = int_type->get_backend(gogo);
4653 4646 : fields[1].location = fields[0].location;
4654 :
4655 4646 : backend_string_type = gogo->backend()->struct_type(fields);
4656 4646 : }
4657 11311 : return backend_string_type;
4658 : }
4659 :
4660 : // The type descriptor for the string type.
4661 :
4662 : Expression*
4663 3027 : String_type::do_type_descriptor(Gogo* gogo, Named_type* name)
4664 : {
4665 3027 : if (name != NULL)
4666 3027 : return this->plain_type_descriptor(gogo, RUNTIME_TYPE_KIND_STRING, name);
4667 : else
4668 : {
4669 0 : Named_object* no = gogo->lookup_global("string");
4670 0 : go_assert(no != NULL);
4671 0 : return Type::type_descriptor(gogo, no->type_value());
4672 : }
4673 : }
4674 :
4675 : // We should not be asked for the reflection string of a basic type.
4676 :
4677 : void
4678 0 : String_type::do_reflection(Gogo*, std::string* ret) const
4679 : {
4680 0 : ret->append("string");
4681 0 : }
4682 :
4683 : // Make a string type.
4684 :
4685 : Type*
4686 103826699 : Type::make_string_type()
4687 : {
4688 103826699 : static String_type string_type;
4689 103826699 : return &string_type;
4690 : }
4691 :
4692 : // The named type "string".
4693 :
4694 : static Named_type* named_string_type;
4695 :
4696 : // Get the named type "string".
4697 :
4698 : Named_type*
4699 77215 : Type::lookup_string_type()
4700 : {
4701 77215 : return named_string_type;
4702 : }
4703 :
4704 : // Make the named type string.
4705 :
4706 : Named_type*
4707 4646 : Type::make_named_string_type()
4708 : {
4709 4646 : Type* string_type = Type::make_string_type();
4710 4646 : Named_object* named_object =
4711 4646 : Named_object::make_type("string", NULL, string_type,
4712 : Linemap::predeclared_location());
4713 4646 : Named_type* named_type = named_object->type_value();
4714 4646 : named_string_type = named_type;
4715 4646 : return named_type;
4716 : }
4717 :
4718 : // The sink type. This is the type of the blank identifier _. Any
4719 : // type may be assigned to it.
4720 :
4721 : class Sink_type : public Type
4722 : {
4723 : public:
4724 1805 : Sink_type()
4725 3610 : : Type(TYPE_SINK)
4726 : { }
4727 :
4728 : protected:
4729 : void
4730 0 : do_message_name(std::string* ret) const
4731 0 : { ret->append("<SINK>"); }
4732 :
4733 : bool
4734 0 : do_compare_is_identity(Gogo*)
4735 0 : { return false; }
4736 :
4737 : Btype*
4738 0 : do_get_backend(Gogo* gogo)
4739 : {
4740 0 : go_assert(saw_errors());
4741 0 : return gogo->backend()->error_type();
4742 : }
4743 :
4744 : Expression*
4745 0 : do_type_descriptor(Gogo*, Named_type*)
4746 0 : { go_unreachable(); }
4747 :
4748 : void
4749 0 : do_reflection(Gogo*, std::string*) const
4750 0 : { go_unreachable(); }
4751 :
4752 : void
4753 0 : do_mangled_name(Gogo*, std::string*, bool*) const
4754 0 : { go_unreachable(); }
4755 : };
4756 :
4757 : // Make the sink type.
4758 :
4759 : Type*
4760 189204 : Type::make_sink_type()
4761 : {
4762 189204 : static Sink_type sink_type;
4763 189204 : return &sink_type;
4764 : }
4765 :
4766 : // Class Function_type.
4767 :
4768 : // Message name.
4769 :
4770 : void
4771 77 : Function_type::do_message_name(std::string* ret) const
4772 : {
4773 77 : ret->append("func");
4774 77 : if (this->receiver_ != NULL)
4775 : {
4776 0 : ret->append(" (receiver ");
4777 0 : this->append_message_name(this->receiver_->type(), ret);
4778 0 : ret->append(") ");
4779 : }
4780 77 : this->append_signature(ret);
4781 77 : }
4782 :
4783 : // Append just the signature to RET.
4784 :
4785 : void
4786 116 : Function_type::append_signature(std::string* ret) const
4787 : {
4788 116 : ret->push_back('(');
4789 116 : if (this->parameters_ != NULL)
4790 : {
4791 69 : bool first = true;
4792 69 : for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
4793 152 : p != this->parameters_->end();
4794 83 : ++p)
4795 : {
4796 83 : if (first)
4797 : first = false;
4798 : else
4799 14 : ret->append(", ");
4800 83 : this->append_message_name(p->type(), ret);
4801 : }
4802 : }
4803 116 : ret->push_back(')');
4804 :
4805 116 : if (this->results_ != NULL)
4806 : {
4807 22 : if (this->results_->size() == 1)
4808 : {
4809 22 : ret->push_back(' ');
4810 22 : this->append_message_name(this->results_->front().type(), ret);
4811 : }
4812 : else
4813 : {
4814 0 : ret->append(" (");
4815 0 : bool first = true;
4816 0 : for (Typed_identifier_list::const_iterator p =
4817 0 : this->results_->begin();
4818 0 : p != this->results_->end();
4819 0 : ++p)
4820 : {
4821 0 : if (first)
4822 : first = false;
4823 : else
4824 0 : ret->append(", ");
4825 0 : this->append_message_name(p->type(), ret);
4826 : }
4827 0 : ret->push_back(')');
4828 : }
4829 : }
4830 116 : }
4831 :
4832 : // Traversal.
4833 :
4834 : int
4835 85952374 : Function_type::do_traverse(Traverse* traverse)
4836 : {
4837 85952374 : if (this->receiver_ != NULL
4838 85952374 : && Type::traverse(this->receiver_->type(), traverse) == TRAVERSE_EXIT)
4839 : return TRAVERSE_EXIT;
4840 85951648 : if (this->parameters_ != NULL
4841 85951648 : && this->parameters_->traverse(traverse) == TRAVERSE_EXIT)
4842 : return TRAVERSE_EXIT;
4843 85950658 : if (this->results_ != NULL
4844 85950658 : && this->results_->traverse(traverse) == TRAVERSE_EXIT)
4845 : return TRAVERSE_EXIT;
4846 : return TRAVERSE_CONTINUE;
4847 : }
4848 :
4849 : // Returns whether T is a valid redeclaration of this type. If this
4850 : // returns false, and REASON is not NULL, *REASON may be set to a
4851 : // brief explanation of why it returned false.
4852 :
4853 : bool
4854 106821 : Function_type::is_valid_redeclaration(const Function_type* t,
4855 : std::string* reason) const
4856 : {
4857 106821 : if (!this->is_identical(t, false, COMPARE_TAGS, reason))
4858 : return false;
4859 :
4860 : // A redeclaration of a function is required to use the same names
4861 : // for the receiver and parameters.
4862 106821 : if (this->receiver() != NULL
4863 106821 : && this->receiver()->name() != t->receiver()->name())
4864 : {
4865 0 : if (reason != NULL)
4866 0 : *reason = "receiver name changed";
4867 0 : return false;
4868 : }
4869 :
4870 106821 : const Typed_identifier_list* parms1 = this->parameters();
4871 106821 : const Typed_identifier_list* parms2 = t->parameters();
4872 106821 : if (parms1 != NULL)
4873 : {
4874 103370 : Typed_identifier_list::const_iterator p1 = parms1->begin();
4875 103370 : for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4876 305971 : p2 != parms2->end();
4877 202601 : ++p2, ++p1)
4878 : {
4879 202601 : if (p1->name() != p2->name())
4880 : {
4881 0 : if (reason != NULL)
4882 0 : *reason = "parameter name changed";
4883 0 : return false;
4884 : }
4885 :
4886 : // This is called at parse time, so we may have unknown
4887 : // types.
4888 202601 : Type* t1 = p1->type()->forwarded();
4889 202601 : Type* t2 = p2->type()->forwarded();
4890 202601 : if (t1 != t2
4891 202601 : && t1->forward_declaration_type() != NULL
4892 202601 : && (t2->forward_declaration_type() == NULL
4893 0 : || (t1->forward_declaration_type()->named_object()
4894 0 : != t2->forward_declaration_type()->named_object())))
4895 0 : return false;
4896 : }
4897 : }
4898 :
4899 106821 : const Typed_identifier_list* results1 = this->results();
4900 106821 : const Typed_identifier_list* results2 = t->results();
4901 106821 : if (results1 != NULL)
4902 : {
4903 103411 : Typed_identifier_list::const_iterator res1 = results1->begin();
4904 103411 : for (Typed_identifier_list::const_iterator res2 = results2->begin();
4905 207655 : res2 != results2->end();
4906 104244 : ++res2, ++res1)
4907 : {
4908 104244 : if (res1->name() != res2->name())
4909 : {
4910 0 : if (reason != NULL)
4911 0 : *reason = "result name changed";
4912 0 : return false;
4913 : }
4914 :
4915 : // This is called at parse time, so we may have unknown
4916 : // types.
4917 104244 : Type* t1 = res1->type()->forwarded();
4918 104244 : Type* t2 = res2->type()->forwarded();
4919 104244 : if (t1 != t2
4920 104244 : && t1->forward_declaration_type() != NULL
4921 104244 : && (t2->forward_declaration_type() == NULL
4922 0 : || (t1->forward_declaration_type()->named_object()
4923 0 : != t2->forward_declaration_type()->named_object())))
4924 0 : return false;
4925 : }
4926 : }
4927 :
4928 : return true;
4929 : }
4930 :
4931 : // Check whether T is the same as this type.
4932 :
4933 : bool
4934 1742613 : Function_type::is_identical(const Function_type* t, bool ignore_receiver,
4935 : int flags, std::string* reason) const
4936 : {
4937 1742613 : if (this->is_backend_function_type() != t->is_backend_function_type())
4938 : return false;
4939 :
4940 1742613 : if (!ignore_receiver)
4941 : {
4942 560072 : const Typed_identifier* r1 = this->receiver();
4943 560072 : const Typed_identifier* r2 = t->receiver();
4944 560072 : if ((r1 != NULL) != (r2 != NULL))
4945 : {
4946 0 : if (reason != NULL)
4947 0 : *reason = _("different receiver types");
4948 0 : return false;
4949 : }
4950 560072 : if (r1 != NULL)
4951 : {
4952 593 : if (!Type::are_identical(r1->type(), r2->type(), flags, reason))
4953 : {
4954 0 : if (reason != NULL && !reason->empty())
4955 0 : *reason = "receiver: " + *reason;
4956 0 : return false;
4957 : }
4958 : }
4959 : }
4960 :
4961 1742613 : const Typed_identifier_list* parms1 = this->parameters();
4962 1742613 : if (parms1 != NULL && parms1->empty())
4963 : parms1 = NULL;
4964 1742613 : const Typed_identifier_list* parms2 = t->parameters();
4965 1742613 : if (parms2 != NULL && parms2->empty())
4966 : parms2 = NULL;
4967 1742613 : if ((parms1 != NULL) != (parms2 != NULL))
4968 : {
4969 245 : if (reason != NULL)
4970 224 : *reason = _("different number of parameters");
4971 245 : return false;
4972 : }
4973 1742368 : if (parms1 != NULL)
4974 : {
4975 776178 : Typed_identifier_list::const_iterator p1 = parms1->begin();
4976 776178 : for (Typed_identifier_list::const_iterator p2 = parms2->begin();
4977 1896485 : p2 != parms2->end();
4978 1120307 : ++p2, ++p1)
4979 : {
4980 1124495 : if (p1 == parms1->end())
4981 : {
4982 3005 : if (reason != NULL)
4983 0 : *reason = _("different number of parameters");
4984 3005 : return false;
4985 : }
4986 :
4987 1121490 : if (!Type::are_identical(p1->type(), p2->type(), flags, NULL))
4988 : {
4989 1183 : if (reason != NULL)
4990 51 : *reason = _("different parameter types");
4991 1183 : return false;
4992 : }
4993 : }
4994 771990 : if (p1 != parms1->end())
4995 : {
4996 5782 : if (reason != NULL)
4997 0 : *reason = _("different number of parameters");
4998 5782 : return false;
4999 : }
5000 : }
5001 :
5002 1732398 : if (this->is_varargs() != t->is_varargs())
5003 : {
5004 0 : if (reason != NULL)
5005 0 : *reason = _("different varargs");
5006 0 : return false;
5007 : }
5008 :
5009 1732398 : const Typed_identifier_list* results1 = this->results();
5010 1732398 : if (results1 != NULL && results1->empty())
5011 : results1 = NULL;
5012 1732398 : const Typed_identifier_list* results2 = t->results();
5013 1732398 : if (results2 != NULL && results2->empty())
5014 : results2 = NULL;
5015 1732398 : if ((results1 != NULL) != (results2 != NULL))
5016 : {
5017 22 : if (reason != NULL)
5018 6 : *reason = _("different number of results");
5019 22 : return false;
5020 : }
5021 1732376 : if (results1 != NULL)
5022 : {
5023 1171563 : Typed_identifier_list::const_iterator res1 = results1->begin();
5024 1171563 : for (Typed_identifier_list::const_iterator res2 = results2->begin();
5025 2461204 : res2 != results2->end();
5026 1289641 : ++res2, ++res1)
5027 : {
5028 1297184 : if (res1 == results1->end())
5029 : {
5030 0 : if (reason != NULL)
5031 0 : *reason = _("different number of results");
5032 0 : return false;
5033 : }
5034 :
5035 1297184 : if (!Type::are_identical(res1->type(), res2->type(), flags, NULL))
5036 : {
5037 7543 : if (reason != NULL)
5038 6523 : *reason = _("different result types");
5039 7543 : return false;
5040 : }
5041 : }
5042 1164020 : if (res1 != results1->end())
5043 : {
5044 0 : if (reason != NULL)
5045 0 : *reason = _("different number of results");
5046 0 : return false;
5047 : }
5048 : }
5049 :
5050 : return true;
5051 : }
5052 :
5053 : // Hash code.
5054 :
5055 : unsigned int
5056 931173 : Function_type::do_hash_for_method(Gogo* gogo, int flags) const
5057 : {
5058 931173 : unsigned int ret = 0;
5059 : // We ignore the receiver type for hash codes, because we need to
5060 : // get the same hash code for a method in an interface and a method
5061 : // declared for a type. The former will not have a receiver.
5062 931173 : if (this->parameters_ != NULL)
5063 : {
5064 722703 : int shift = 1;
5065 722703 : for (Typed_identifier_list::const_iterator p = this->parameters_->begin();
5066 1965186 : p != this->parameters_->end();
5067 1242483 : ++p, ++shift)
5068 1242483 : ret += p->type()->hash_for_method(gogo, flags) << shift;
5069 : }
5070 931173 : if (this->results_ != NULL)
5071 : {
5072 674497 : int shift = 2;
5073 674497 : for (Typed_identifier_list::const_iterator p = this->results_->begin();
5074 1507836 : p != this->results_->end();
5075 833339 : ++p, ++shift)
5076 833339 : ret += p->type()->hash_for_method(gogo, flags) << shift;
5077 : }
5078 931173 : if (this->is_varargs_)
5079 10365 : ret += 1;
5080 931173 : ret <<= 4;
5081 931173 : return ret;
5082 : }
5083 :
5084 : // Hash result parameters.
5085 :
5086 : unsigned int
5087 101058 : Function_type::Results_hash::operator()(const Typed_identifier_list* t) const
5088 : {
5089 101058 : unsigned int hash = 0;
5090 101058 : for (Typed_identifier_list::const_iterator p = t->begin();
5091 316500 : p != t->end();
5092 215442 : ++p)
5093 : {
5094 215442 : hash <<= 2;
5095 215442 : hash = Gogo::hash_string(p->name(), hash);
5096 215442 : hash += p->type()->hash_for_method(NULL, Type::COMPARE_TAGS);
5097 : }
5098 101058 : return hash;
5099 : }
5100 :
5101 : // Compare result parameters so that can map identical result
5102 : // parameters to a single struct type.
5103 :
5104 : bool
5105 67059 : Function_type::Results_equal::operator()(const Typed_identifier_list* a,
5106 : const Typed_identifier_list* b) const
5107 : {
5108 67059 : if (a->size() != b->size())
5109 : return false;
5110 : Typed_identifier_list::const_iterator pa = a->begin();
5111 : for (Typed_identifier_list::const_iterator pb = b->begin();
5112 206372 : pb != b->end();
5113 139313 : ++pa, ++pb)
5114 : {
5115 140256 : if (pa->name() != pb->name()
5116 140256 : || !Type::are_identical(pa->type(), pb->type(), Type::COMPARE_TAGS,
5117 : NULL))
5118 943 : return false;
5119 : }
5120 : return true;
5121 : }
5122 :
5123 : // Hash from results to a backend struct type.
5124 :
5125 : Function_type::Results_structs Function_type::results_structs;
5126 :
5127 : // Get the backend representation for a function type.
5128 :
5129 : Btype*
5130 1093332 : Function_type::get_backend_fntype(Gogo* gogo)
5131 : {
5132 1093332 : if (this->fnbtype_ == NULL)
5133 : {
5134 883623 : Backend::Btyped_identifier breceiver;
5135 883623 : if (this->receiver_ != NULL)
5136 : {
5137 198072 : breceiver.name = Gogo::unpack_hidden_name(this->receiver_->name());
5138 :
5139 : // We always pass the address of the receiver parameter, in
5140 : // order to make interface calls work with unknown types,
5141 : // except for direct interface types where the interface call
5142 : // actually passes the underlying pointer of the value.
5143 198072 : Type* rtype = this->receiver_->type();
5144 198072 : if (rtype->points_to() == NULL)
5145 : {
5146 32180 : if (rtype->is_direct_iface_type())
5147 2309 : rtype = Type::make_pointer_type(Type::make_void_type());
5148 : else
5149 29871 : rtype = Type::make_pointer_type(rtype);
5150 : }
5151 198072 : breceiver.btype = rtype->get_backend(gogo);
5152 198072 : breceiver.location = this->receiver_->location();
5153 : }
5154 :
5155 883623 : std::vector<Backend::Btyped_identifier> bparameters;
5156 883623 : if (this->parameters_ != NULL)
5157 : {
5158 695574 : bparameters.resize(this->parameters_->size());
5159 695574 : size_t i = 0;
5160 695574 : for (Typed_identifier_list::const_iterator p =
5161 1982777 : this->parameters_->begin(); p != this->parameters_->end();
5162 1287203 : ++p, ++i)
5163 : {
5164 1287203 : bparameters[i].name = Gogo::unpack_hidden_name(p->name());
5165 1287203 : bparameters[i].btype = p->type()->get_backend(gogo);
5166 1287203 : bparameters[i].location = p->location();
5167 : }
5168 695574 : go_assert(i == bparameters.size());
5169 : }
5170 :
5171 883623 : std::vector<Backend::Btyped_identifier> bresults;
5172 883623 : Btype* bresult_struct = NULL;
5173 883623 : if (this->results_ != NULL)
5174 : {
5175 688328 : bresults.resize(this->results_->size());
5176 688328 : size_t i = 0;
5177 688328 : for (Typed_identifier_list::const_iterator p =
5178 688328 : this->results_->begin();
5179 1491040 : p != this->results_->end();
5180 802712 : ++p, ++i)
5181 : {
5182 802712 : bresults[i].name = Gogo::unpack_hidden_name(p->name());
5183 802712 : bresults[i].btype = p->type()->get_backend(gogo);
5184 802712 : bresults[i].location = p->location();
5185 : }
5186 688328 : go_assert(i == bresults.size());
5187 :
5188 688328 : if (this->results_->size() > 1)
5189 : {
5190 : // Use the same results struct for all functions that
5191 : // return the same set of results. This is useful to
5192 : // unify calls to interface methods with other calls.
5193 101058 : std::pair<Typed_identifier_list*, Btype*> val;
5194 101058 : val.first = this->results_;
5195 101058 : val.second = NULL;
5196 101058 : std::pair<Results_structs::iterator, bool> ins =
5197 101058 : Function_type::results_structs.insert(val);
5198 101058 : if (ins.second)
5199 : {
5200 : // Build a new struct type.
5201 34942 : Struct_field_list* sfl = new Struct_field_list;
5202 111073 : for (Typed_identifier_list::const_iterator p =
5203 34942 : this->results_->begin();
5204 111073 : p != this->results_->end();
5205 76131 : ++p)
5206 : {
5207 76131 : Typed_identifier tid = *p;
5208 76131 : if (tid.name().empty())
5209 86458 : tid = Typed_identifier("UNNAMED", tid.type(),
5210 86458 : tid.location());
5211 76131 : sfl->push_back(Struct_field(tid));
5212 76131 : }
5213 34942 : Struct_type* st = Type::make_struct_type(sfl,
5214 : this->location());
5215 34942 : st->set_is_struct_incomparable();
5216 34942 : st->set_is_results_struct();
5217 34942 : ins.first->second = st->get_backend(gogo);
5218 : }
5219 101058 : bresult_struct = ins.first->second;
5220 : }
5221 : }
5222 :
5223 883623 : this->fnbtype_ = gogo->backend()->function_type(breceiver, bparameters,
5224 : bresults, bresult_struct,
5225 : this->location());
5226 :
5227 883623 : }
5228 :
5229 1093332 : return this->fnbtype_;
5230 : }
5231 :
5232 : // Get the backend representation for a Go function type.
5233 :
5234 : Btype*
5235 186845 : Function_type::do_get_backend(Gogo* gogo)
5236 : {
5237 : // When we do anything with a function value other than call it, it
5238 : // is represented as a pointer to a struct whose first field is the
5239 : // actual function. So that is what we return as the type of a Go
5240 : // function.
5241 :
5242 186845 : Location loc = this->location();
5243 186845 : Btype* struct_type =
5244 186845 : gogo->backend()->placeholder_struct_type("__go_descriptor", loc);
5245 186845 : Btype* ptr_struct_type = gogo->backend()->pointer_type(struct_type);
5246 :
5247 186845 : std::vector<Backend::Btyped_identifier> fields(1);
5248 186845 : fields[0].name = "code";
5249 186845 : fields[0].btype = this->get_backend_fntype(gogo);
5250 186845 : fields[0].location = loc;
5251 186845 : if (!gogo->backend()->set_placeholder_struct_type(struct_type, fields))
5252 1 : return gogo->backend()->error_type();
5253 : return ptr_struct_type;
5254 186845 : }
5255 :
5256 : // The type of a function type descriptor.
5257 :
5258 : Type*
5259 98275 : Function_type::make_function_type_descriptor_type()
5260 : {
5261 98275 : static Type* ret;
5262 98275 : if (ret == NULL)
5263 : {
5264 4646 : Type* tdt = Type::make_type_descriptor_type();
5265 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
5266 :
5267 4646 : Type* bool_type = Type::lookup_bool_type();
5268 :
5269 4646 : Type* slice_type = Type::make_array_type(ptdt, NULL);
5270 :
5271 4646 : Struct_type* s = Type::make_builtin_struct_type(4,
5272 : "", tdt,
5273 : "dotdotdot", bool_type,
5274 : "in", slice_type,
5275 : "out", slice_type);
5276 :
5277 4646 : ret = Type::make_builtin_named_type("FuncType", s);
5278 : }
5279 :
5280 98275 : return ret;
5281 : }
5282 :
5283 : // The type descriptor for a function type.
5284 :
5285 : Expression*
5286 93629 : Function_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5287 : {
5288 93629 : Location bloc = Linemap::predeclared_location();
5289 :
5290 93629 : Type* ftdt = Function_type::make_function_type_descriptor_type();
5291 :
5292 187258 : const Struct_field_list* fields = ftdt->struct_type()->fields();
5293 :
5294 93629 : Expression_list* vals = new Expression_list();
5295 93629 : vals->reserve(4);
5296 :
5297 93629 : Struct_field_list::const_iterator p = fields->begin();
5298 93629 : go_assert(p->is_field_name("_type"));
5299 93629 : vals->push_back(this->type_descriptor_constructor(gogo,
5300 : RUNTIME_TYPE_KIND_FUNC,
5301 : name, NULL, true));
5302 :
5303 93629 : ++p;
5304 93629 : go_assert(p->is_field_name("dotdotdot"));
5305 93629 : vals->push_back(Expression::make_boolean(this->is_varargs(), bloc));
5306 :
5307 93629 : ++p;
5308 93629 : go_assert(p->is_field_name("in"));
5309 93629 : vals->push_back(this->type_descriptor_params(p->type(), this->receiver(),
5310 : this->parameters()));
5311 :
5312 93629 : ++p;
5313 93629 : go_assert(p->is_field_name("out"));
5314 93629 : vals->push_back(this->type_descriptor_params(p->type(), NULL,
5315 : this->results()));
5316 :
5317 93629 : ++p;
5318 93629 : go_assert(p == fields->end());
5319 :
5320 93629 : return Expression::make_struct_composite_literal(ftdt, vals, bloc);
5321 : }
5322 :
5323 : // Return a composite literal for the parameters or results of a type
5324 : // descriptor.
5325 :
5326 : Expression*
5327 187258 : Function_type::type_descriptor_params(Type* params_type,
5328 : const Typed_identifier* receiver,
5329 : const Typed_identifier_list* params)
5330 : {
5331 187258 : Location bloc = Linemap::predeclared_location();
5332 :
5333 187258 : if (receiver == NULL && params == NULL)
5334 29513 : return Expression::make_slice_composite_literal(params_type, NULL, bloc);
5335 :
5336 157745 : Expression_list* vals = new Expression_list();
5337 473235 : vals->reserve((params == NULL ? 0 : params->size())
5338 157745 : + (receiver != NULL ? 1 : 0));
5339 :
5340 157745 : if (receiver != NULL)
5341 0 : vals->push_back(Expression::make_type_descriptor(receiver->type(), bloc));
5342 :
5343 157745 : if (params != NULL)
5344 : {
5345 157745 : for (Typed_identifier_list::const_iterator p = params->begin();
5346 410841 : p != params->end();
5347 253096 : ++p)
5348 253096 : vals->push_back(Expression::make_type_descriptor(p->type(), bloc));
5349 : }
5350 :
5351 157745 : return Expression::make_slice_composite_literal(params_type, vals, bloc);
5352 : }
5353 :
5354 : // The reflection string.
5355 :
5356 : void
5357 97879 : Function_type::do_reflection(Gogo* gogo, std::string* ret) const
5358 : {
5359 : // FIXME: Turn this off until we straighten out the type of the
5360 : // struct field used in a go statement which calls a method.
5361 : // go_assert(this->receiver_ == NULL);
5362 :
5363 97879 : ret->append("func");
5364 :
5365 97879 : if (this->receiver_ != NULL)
5366 : {
5367 0 : ret->push_back('(');
5368 0 : this->append_reflection(this->receiver_->type(), gogo, ret);
5369 0 : ret->push_back(')');
5370 : }
5371 :
5372 97879 : ret->push_back('(');
5373 97879 : const Typed_identifier_list* params = this->parameters();
5374 97879 : if (params != NULL)
5375 : {
5376 87401 : bool is_varargs = this->is_varargs_;
5377 248977 : for (Typed_identifier_list::const_iterator p = params->begin();
5378 248977 : p != params->end();
5379 161576 : ++p)
5380 : {
5381 161576 : if (p != params->begin())
5382 74307 : ret->append(", ");
5383 161576 : if (!is_varargs || p + 1 != params->end())
5384 160225 : this->append_reflection(p->type(), gogo, ret);
5385 : else
5386 : {
5387 1351 : ret->append("...");
5388 2702 : this->append_reflection(p->type()->array_type()->element_type(),
5389 : gogo, ret);
5390 : }
5391 : }
5392 : }
5393 97879 : ret->push_back(')');
5394 :
5395 97879 : const Typed_identifier_list* results = this->results();
5396 97879 : if (results != NULL && !results->empty())
5397 : {
5398 75554 : if (results->size() == 1)
5399 55916 : ret->push_back(' ');
5400 : else
5401 19638 : ret->append(" (");
5402 75554 : for (Typed_identifier_list::const_iterator p = results->begin();
5403 174239 : p != results->end();
5404 98685 : ++p)
5405 : {
5406 98685 : if (p != results->begin())
5407 23131 : ret->append(", ");
5408 98685 : this->append_reflection(p->type(), gogo, ret);
5409 : }
5410 75554 : if (results->size() > 1)
5411 19638 : ret->push_back(')');
5412 : }
5413 97879 : }
5414 :
5415 : // Export a function type.
5416 :
5417 : void
5418 24165 : Function_type::do_export(Export* exp) const
5419 : {
5420 : // We don't write out the receiver. The only function types which
5421 : // should have a receiver are the ones associated with explicitly
5422 : // defined methods. For those the receiver type is written out by
5423 : // Function::export_func.
5424 :
5425 24165 : exp->write_c_string("(");
5426 24165 : bool first = true;
5427 24165 : if (this->parameters_ != NULL)
5428 : {
5429 20263 : bool is_varargs = this->is_varargs_;
5430 36162 : for (Typed_identifier_list::const_iterator p =
5431 20263 : this->parameters_->begin();
5432 56425 : p != this->parameters_->end();
5433 36162 : ++p)
5434 : {
5435 36162 : if (first)
5436 : first = false;
5437 : else
5438 15899 : exp->write_c_string(", ");
5439 : // The hash for a function type ignores parameter names, so
5440 : // we don't want to write them out here. If we did write
5441 : // them out, we could get spurious changes in export data
5442 : // when recompiling a package.
5443 36162 : exp->write_name("");
5444 36162 : exp->write_c_string(" ");
5445 36162 : if (!is_varargs || p + 1 != this->parameters_->end())
5446 35790 : exp->write_type(p->type());
5447 : else
5448 : {
5449 372 : exp->write_c_string("...");
5450 744 : exp->write_type(p->type()->array_type()->element_type());
5451 : }
5452 : }
5453 : }
5454 24165 : exp->write_c_string(")");
5455 :
5456 24165 : const Typed_identifier_list* results = this->results_;
5457 24165 : if (results != NULL)
5458 : {
5459 16520 : exp->write_c_string(" ");
5460 16520 : if (results->size() == 1)
5461 14925 : exp->write_type(results->begin()->type());
5462 : else
5463 : {
5464 1595 : first = true;
5465 1595 : exp->write_c_string("(");
5466 4874 : for (Typed_identifier_list::const_iterator p = results->begin();
5467 4874 : p != results->end();
5468 3279 : ++p)
5469 : {
5470 3279 : if (first)
5471 : first = false;
5472 : else
5473 1684 : exp->write_c_string(", ");
5474 3279 : exp->write_name("");
5475 3279 : exp->write_c_string(" ");
5476 3279 : exp->write_type(p->type());
5477 : }
5478 1595 : exp->write_c_string(")");
5479 : }
5480 : }
5481 24165 : }
5482 :
5483 : // Import a function type.
5484 :
5485 : Function_type*
5486 68687 : Function_type::do_import(Import* imp)
5487 : {
5488 68687 : imp->require_c_string("(");
5489 68687 : Typed_identifier_list* parameters;
5490 68687 : bool is_varargs = false;
5491 68687 : if (imp->peek_char() == ')')
5492 : parameters = NULL;
5493 : else
5494 : {
5495 58721 : parameters = new Typed_identifier_list();
5496 117575 : while (true)
5497 : {
5498 88148 : std::string name = imp->read_name();
5499 88148 : imp->require_c_string(" ");
5500 :
5501 88148 : if (imp->match_c_string("..."))
5502 : {
5503 485 : imp->advance(3);
5504 485 : is_varargs = true;
5505 : }
5506 :
5507 88148 : Type* ptype = imp->read_type();
5508 88148 : if (is_varargs)
5509 485 : ptype = Type::make_array_type(ptype, NULL);
5510 88148 : parameters->push_back(Typed_identifier(name, ptype,
5511 176296 : imp->location()));
5512 88148 : if (imp->peek_char() != ',')
5513 : break;
5514 29427 : go_assert(!is_varargs);
5515 29427 : imp->require_c_string(", ");
5516 88148 : }
5517 : }
5518 68687 : imp->require_c_string(")");
5519 :
5520 68687 : Typed_identifier_list* results;
5521 68687 : if (imp->peek_char() != ' ')
5522 : results = NULL;
5523 : else
5524 : {
5525 39746 : imp->advance(1);
5526 39746 : results = new Typed_identifier_list;
5527 39746 : if (imp->peek_char() != '(')
5528 : {
5529 34470 : Type* rtype = imp->read_type();
5530 68940 : results->push_back(Typed_identifier("", rtype, imp->location()));
5531 : }
5532 : else
5533 : {
5534 5276 : imp->advance(1);
5535 16820 : while (true)
5536 : {
5537 11048 : std::string name = imp->read_name();
5538 11048 : imp->require_c_string(" ");
5539 11048 : Type* rtype = imp->read_type();
5540 11048 : results->push_back(Typed_identifier(name, rtype,
5541 22096 : imp->location()));
5542 11048 : if (imp->peek_char() != ',')
5543 : break;
5544 5772 : imp->require_c_string(", ");
5545 5772 : }
5546 5276 : imp->require_c_string(")");
5547 : }
5548 : }
5549 :
5550 68687 : Function_type* ret = Type::make_function_type(NULL, parameters, results,
5551 68687 : imp->location());
5552 68687 : if (is_varargs)
5553 485 : ret->set_is_varargs();
5554 68687 : return ret;
5555 : }
5556 :
5557 : // Make a copy of a function type without a receiver.
5558 :
5559 : Function_type*
5560 2718145 : Function_type::copy_without_receiver() const
5561 : {
5562 2718145 : go_assert(this->is_method());
5563 5436290 : Function_type *ret = Type::make_function_type(NULL, this->parameters_,
5564 2718145 : this->results_,
5565 : this->location_);
5566 2718145 : if (this->is_varargs())
5567 381238 : ret->set_is_varargs();
5568 2718145 : if (this->is_builtin())
5569 0 : ret->set_is_builtin();
5570 2718145 : return ret;
5571 : }
5572 :
5573 : // Make a copy of a function type with a receiver.
5574 :
5575 : Function_type*
5576 2110 : Function_type::copy_with_receiver(Type* receiver_type) const
5577 : {
5578 2110 : go_assert(!this->is_method());
5579 2110 : Typed_identifier* receiver = new Typed_identifier("", receiver_type,
5580 4220 : this->location_);
5581 4220 : Function_type* ret = Type::make_function_type(receiver, this->parameters_,
5582 2110 : this->results_,
5583 : this->location_);
5584 2110 : if (this->is_varargs_)
5585 1 : ret->set_is_varargs();
5586 2110 : return ret;
5587 : }
5588 :
5589 : // Make a copy of a function type with the receiver as the first
5590 : // parameter.
5591 :
5592 : Function_type*
5593 299629 : Function_type::copy_with_receiver_as_param(bool want_pointer_receiver) const
5594 : {
5595 299629 : go_assert(this->is_method());
5596 299629 : Typed_identifier_list* new_params = new Typed_identifier_list();
5597 299629 : Type* rtype = this->receiver_->type();
5598 299629 : if (want_pointer_receiver)
5599 24120 : rtype = Type::make_pointer_type(rtype);
5600 299629 : Typed_identifier receiver(this->receiver_->name(), rtype,
5601 299629 : this->receiver_->location());
5602 299629 : new_params->push_back(receiver);
5603 299629 : const Typed_identifier_list* orig_params = this->parameters_;
5604 299629 : if (orig_params != NULL && !orig_params->empty())
5605 : {
5606 200453 : for (Typed_identifier_list::const_iterator p = orig_params->begin();
5607 336277 : p != orig_params->end();
5608 200453 : ++p)
5609 200453 : new_params->push_back(*p);
5610 : }
5611 599258 : Function_type* ret = Type::make_function_type(NULL, new_params,
5612 299629 : this->results_,
5613 : this->location_);
5614 299629 : if (this->is_varargs_)
5615 5410 : ret->set_is_varargs();
5616 299629 : return ret;
5617 299629 : }
5618 :
5619 : // Make a copy of a function type ignoring any receiver and adding a
5620 : // closure parameter.
5621 :
5622 : Function_type*
5623 708 : Function_type::copy_with_names() const
5624 : {
5625 708 : Typed_identifier_list* new_params = new Typed_identifier_list();
5626 708 : const Typed_identifier_list* orig_params = this->parameters_;
5627 708 : if (orig_params != NULL && !orig_params->empty())
5628 : {
5629 : static int count;
5630 : char buf[50];
5631 803 : for (Typed_identifier_list::const_iterator p = orig_params->begin();
5632 1227 : p != orig_params->end();
5633 803 : ++p)
5634 : {
5635 803 : snprintf(buf, sizeof buf, "pt.%u", count);
5636 803 : ++count;
5637 1606 : new_params->push_back(Typed_identifier(buf, p->type(),
5638 1606 : p->location()));
5639 : }
5640 : }
5641 :
5642 708 : const Typed_identifier_list* orig_results = this->results_;
5643 708 : Typed_identifier_list* new_results;
5644 708 : if (orig_results == NULL || orig_results->empty())
5645 : new_results = NULL;
5646 : else
5647 : {
5648 308 : new_results = new Typed_identifier_list();
5649 308 : for (Typed_identifier_list::const_iterator p = orig_results->begin();
5650 688 : p != orig_results->end();
5651 380 : ++p)
5652 760 : new_results->push_back(Typed_identifier("", p->type(),
5653 760 : p->location()));
5654 : }
5655 :
5656 708 : return Type::make_function_type(NULL, new_params, new_results,
5657 708 : this->location());
5658 : }
5659 :
5660 : // Make a function type.
5661 :
5662 : Function_type*
5663 7729919 : Type::make_function_type(Typed_identifier* receiver,
5664 : Typed_identifier_list* parameters,
5665 : Typed_identifier_list* results,
5666 : Location location)
5667 : {
5668 7729919 : return new Function_type(receiver, parameters, results, location);
5669 : }
5670 :
5671 : // Make a backend function type.
5672 :
5673 : Backend_function_type*
5674 21756 : Type::make_backend_function_type(Typed_identifier* receiver,
5675 : Typed_identifier_list* parameters,
5676 : Typed_identifier_list* results,
5677 : Location location)
5678 : {
5679 21756 : return new Backend_function_type(receiver, parameters, results, location);
5680 : }
5681 :
5682 : // Class Pointer_type.
5683 :
5684 : // Message name.
5685 :
5686 : void
5687 15484 : Pointer_type::do_message_name(std::string* ret) const
5688 : {
5689 15484 : if (this->to_type_->is_void_type())
5690 0 : ret->append("unsafe.Pointer");
5691 : else
5692 : {
5693 15484 : ret->push_back('*');
5694 15484 : this->append_message_name(this->to_type_, ret);
5695 : }
5696 15484 : }
5697 :
5698 : // Traversal.
5699 :
5700 : int
5701 106006344 : Pointer_type::do_traverse(Traverse* traverse)
5702 : {
5703 106006344 : return Type::traverse(this->to_type_, traverse);
5704 : }
5705 :
5706 : // Hash code.
5707 :
5708 : unsigned int
5709 4579297 : Pointer_type::do_hash_for_method(Gogo* gogo, int flags) const
5710 : {
5711 4579297 : return this->to_type_->hash_for_method(gogo, flags) << 4;
5712 : }
5713 :
5714 : // Get the backend representation for a pointer type.
5715 :
5716 : Btype*
5717 407826 : Pointer_type::do_get_backend(Gogo* gogo)
5718 : {
5719 407826 : Btype* to_btype = this->to_type_->get_backend(gogo);
5720 407826 : return gogo->backend()->pointer_type(to_btype);
5721 : }
5722 :
5723 : // The type of a pointer type descriptor.
5724 :
5725 : Type*
5726 81907 : Pointer_type::make_pointer_type_descriptor_type()
5727 : {
5728 81907 : static Type* ret;
5729 81907 : if (ret == NULL)
5730 : {
5731 4646 : Type* tdt = Type::make_type_descriptor_type();
5732 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
5733 :
5734 4646 : Struct_type* s = Type::make_builtin_struct_type(2,
5735 : "", tdt,
5736 : "elem", ptdt);
5737 :
5738 4646 : ret = Type::make_builtin_named_type("PtrType", s);
5739 : }
5740 :
5741 81907 : return ret;
5742 : }
5743 :
5744 : // The type descriptor for a pointer type.
5745 :
5746 : Expression*
5747 77261 : Pointer_type::do_type_descriptor(Gogo* gogo, Named_type* name)
5748 : {
5749 77261 : if (this->is_unsafe_pointer_type())
5750 : {
5751 0 : go_assert(name != NULL);
5752 0 : return this->plain_type_descriptor(gogo,
5753 : RUNTIME_TYPE_KIND_UNSAFE_POINTER,
5754 0 : name);
5755 : }
5756 : else
5757 : {
5758 77261 : Location bloc = Linemap::predeclared_location();
5759 :
5760 77261 : const Methods* methods;
5761 77261 : Type* deref = this->points_to();
5762 77261 : if (deref->named_type() != NULL)
5763 61101 : methods = deref->named_type()->methods();
5764 16160 : else if (deref->struct_type() != NULL)
5765 1470 : methods = deref->struct_type()->methods();
5766 : else
5767 : methods = NULL;
5768 :
5769 77261 : Type* ptr_tdt = Pointer_type::make_pointer_type_descriptor_type();
5770 :
5771 154522 : const Struct_field_list* fields = ptr_tdt->struct_type()->fields();
5772 :
5773 77261 : Expression_list* vals = new Expression_list();
5774 77261 : vals->reserve(2);
5775 :
5776 77261 : Struct_field_list::const_iterator p = fields->begin();
5777 77261 : go_assert(p->is_field_name("_type"));
5778 77261 : vals->push_back(this->type_descriptor_constructor(gogo,
5779 : RUNTIME_TYPE_KIND_PTR,
5780 : name, methods, false));
5781 :
5782 77261 : ++p;
5783 77261 : go_assert(p->is_field_name("elem"));
5784 77261 : vals->push_back(Expression::make_type_descriptor(deref, bloc));
5785 :
5786 77261 : return Expression::make_struct_composite_literal(ptr_tdt, vals, bloc);
5787 : }
5788 : }
5789 :
5790 : // Reflection string.
5791 :
5792 : void
5793 201435 : Pointer_type::do_reflection(Gogo* gogo, std::string* ret) const
5794 : {
5795 201435 : ret->push_back('*');
5796 201435 : this->append_reflection(this->to_type_, gogo, ret);
5797 201435 : }
5798 :
5799 : // Export.
5800 :
5801 : void
5802 40858 : Pointer_type::do_export(Export* exp) const
5803 : {
5804 40858 : exp->write_c_string("*");
5805 40858 : if (this->is_unsafe_pointer_type())
5806 681 : exp->write_c_string("any");
5807 : else
5808 40177 : exp->write_type(this->to_type_);
5809 40858 : }
5810 :
5811 : // Import.
5812 :
5813 : Pointer_type*
5814 495950 : Pointer_type::do_import(Import* imp)
5815 : {
5816 495950 : imp->require_c_string("*");
5817 495950 : if (imp->match_c_string("any"))
5818 : {
5819 671 : imp->advance(3);
5820 671 : return Type::make_pointer_type(Type::make_void_type());
5821 : }
5822 495279 : Type* to = imp->read_type();
5823 495279 : return Type::make_pointer_type(to);
5824 : }
5825 :
5826 : // Cache of pointer types. Key is "to" type, value is pointer type
5827 : // that points to key.
5828 :
5829 : Type::Pointer_type_table Type::pointer_types;
5830 :
5831 : // A list of placeholder pointer types; items on this list will be either be
5832 : // Pointer_type or Function_type. We keep this so we can ensure they are
5833 : // finalized.
5834 :
5835 : std::vector<Type*> Type::placeholder_pointers;
5836 :
5837 : // Make a pointer type.
5838 :
5839 : Pointer_type*
5840 33092787 : Type::make_pointer_type(Type* to_type)
5841 : {
5842 33092787 : Pointer_type_table::const_iterator p = pointer_types.find(to_type);
5843 33092787 : if (p != pointer_types.end())
5844 31304528 : return p->second;
5845 1788259 : Pointer_type* ret = new Pointer_type(to_type);
5846 1788259 : pointer_types[to_type] = ret;
5847 1788259 : return ret;
5848 : }
5849 :
5850 : // This helper is invoked immediately after named types have been
5851 : // converted, to clean up any unresolved pointer types remaining in
5852 : // the pointer type cache.
5853 : //
5854 : // The motivation for this routine: occasionally the compiler creates
5855 : // some specific pointer type as part of a lowering operation (ex:
5856 : // pointer-to-void), then Type::backend_type_size() is invoked on the
5857 : // type (which creates a Btype placeholder for it), that placeholder
5858 : // passed somewhere along the line to the back end, but since there is
5859 : // no reference to the type in user code, there is never a call to
5860 : // Type::finish_backend for the type (hence the Btype remains as an
5861 : // unresolved placeholder). Calling this routine will clean up such
5862 : // instances.
5863 :
5864 : void
5865 4646 : Type::finish_pointer_types(Gogo* gogo)
5866 : {
5867 : // We don't use begin() and end() because it is possible to add new
5868 : // placeholder pointer types as we finalized existing ones.
5869 256387 : for (size_t i = 0; i < Type::placeholder_pointers.size(); i++)
5870 : {
5871 251741 : Type* typ = Type::placeholder_pointers[i];
5872 251741 : Type_btypes::iterator tbti = Type::type_btypes.find(typ);
5873 251741 : if (tbti != Type::type_btypes.end() && tbti->second.is_placeholder)
5874 : {
5875 169137 : typ->finish_backend(gogo, tbti->second.btype);
5876 169137 : tbti->second.is_placeholder = false;
5877 : }
5878 : }
5879 4646 : }
5880 :
5881 : // Class Nil_type.
5882 :
5883 : // Get the backend representation of a nil type. FIXME: Is this ever
5884 : // actually called?
5885 :
5886 : Btype*
5887 1169 : Nil_type::do_get_backend(Gogo* gogo)
5888 : {
5889 1169 : return gogo->backend()->pointer_type(gogo->backend()->void_type());
5890 : }
5891 :
5892 : // Make the nil type.
5893 :
5894 : Type*
5895 2024013 : Type::make_nil_type()
5896 : {
5897 2024013 : static Nil_type singleton_nil_type;
5898 2024013 : return &singleton_nil_type;
5899 : }
5900 :
5901 : // The type of a function call which returns multiple values. This is
5902 : // really a struct, but we don't want to confuse a function call which
5903 : // returns a struct with a function call which returns multiple
5904 : // values.
5905 :
5906 : class Call_multiple_result_type : public Type
5907 : {
5908 : public:
5909 93394 : Call_multiple_result_type()
5910 186788 : : Type(TYPE_CALL_MULTIPLE_RESULT)
5911 : { }
5912 :
5913 : protected:
5914 : void
5915 0 : do_message_name(std::string* ret) const
5916 0 : { ret->append("<call-multiple-result>"); }
5917 :
5918 : bool
5919 0 : do_has_pointer() const
5920 0 : { return false; }
5921 :
5922 : bool
5923 0 : do_compare_is_identity(Gogo*)
5924 0 : { return false; }
5925 :
5926 : Btype*
5927 0 : do_get_backend(Gogo* gogo)
5928 : {
5929 0 : go_assert(saw_errors());
5930 0 : return gogo->backend()->error_type();
5931 : }
5932 :
5933 : Expression*
5934 0 : do_type_descriptor(Gogo*, Named_type*)
5935 : {
5936 0 : go_assert(saw_errors());
5937 0 : return Expression::make_error(Linemap::unknown_location());
5938 : }
5939 :
5940 : void
5941 0 : do_reflection(Gogo*, std::string*) const
5942 0 : { go_assert(saw_errors()); }
5943 :
5944 : void
5945 0 : do_mangled_name(Gogo*, std::string*, bool*) const
5946 0 : { go_assert(saw_errors()); }
5947 : };
5948 :
5949 : // Make a call result type.
5950 :
5951 : Type*
5952 93394 : Type::make_call_multiple_result_type()
5953 : {
5954 93394 : return new Call_multiple_result_type;
5955 : }
5956 :
5957 : // Class Struct_field.
5958 :
5959 : // Get the name of a field.
5960 :
5961 : const std::string&
5962 17345306 : Struct_field::field_name() const
5963 : {
5964 17345306 : const std::string& name(this->typed_identifier_.name());
5965 17345306 : if (!name.empty())
5966 : return name;
5967 : else
5968 : {
5969 : // This is called during parsing, before anything is lowered, so
5970 : // we have to be pretty careful to avoid dereferencing an
5971 : // unknown type name.
5972 562574 : Type* t = this->typed_identifier_.type();
5973 562574 : Type* dt = t;
5974 562574 : if (t->classification() == Type::TYPE_POINTER)
5975 : {
5976 : // Very ugly.
5977 86042 : Pointer_type* ptype = static_cast<Pointer_type*>(t);
5978 86042 : dt = ptype->points_to();
5979 : }
5980 562574 : if (dt->forward_declaration_type() != NULL)
5981 37130 : return dt->forward_declaration_type()->name();
5982 525444 : else if (dt->named_type() != NULL)
5983 : {
5984 : // Note that this can be an alias name.
5985 525444 : return dt->named_type()->name();
5986 : }
5987 0 : else if (t->is_error_type() || dt->is_error_type())
5988 : {
5989 0 : static const std::string error_string = "*error*";
5990 0 : return error_string;
5991 : }
5992 : else
5993 : {
5994 : // Avoid crashing in the erroneous case where T is named but
5995 : // DT is not.
5996 0 : go_assert(t != dt);
5997 0 : if (t->forward_declaration_type() != NULL)
5998 0 : return t->forward_declaration_type()->name();
5999 0 : else if (t->named_type() != NULL)
6000 0 : return t->named_type()->name();
6001 : else
6002 0 : go_unreachable();
6003 : }
6004 : }
6005 : }
6006 :
6007 : // Return whether this field is named NAME.
6008 :
6009 : bool
6010 25063254 : Struct_field::is_field_name(const std::string& name) const
6011 : {
6012 25063254 : const std::string& me(this->typed_identifier_.name());
6013 25063254 : if (!me.empty())
6014 23105425 : return me == name;
6015 : else
6016 : {
6017 1957829 : Type* t = this->typed_identifier_.type();
6018 1957829 : if (t->points_to() != NULL)
6019 880336 : t = t->points_to();
6020 1957829 : Named_type* nt = t->named_type();
6021 1957829 : if (nt != NULL && nt->name() == name)
6022 : return true;
6023 :
6024 : // This is a horrible hack caused by the fact that we don't pack
6025 : // the names of builtin types. FIXME.
6026 2793468 : if (!this->is_imported_
6027 976263 : && nt != NULL
6028 976263 : && nt->is_builtin()
6029 2793468 : && nt->name() == Gogo::unpack_hidden_name(name))
6030 : return true;
6031 :
6032 : return false;
6033 : }
6034 : }
6035 :
6036 : // Return whether this field is an unexported field named NAME.
6037 :
6038 : bool
6039 49 : Struct_field::is_unexported_field_name(Gogo* gogo,
6040 : const std::string& name) const
6041 : {
6042 49 : const std::string& field_name(this->field_name());
6043 98 : if (Gogo::is_hidden_name(field_name)
6044 67 : && name == Gogo::unpack_hidden_name(field_name)
6045 70 : && gogo->pack_hidden_name(name, false) != field_name)
6046 : return true;
6047 :
6048 : // Check for the name of a builtin type. This is like the test in
6049 : // is_field_name, only there we return false if this->is_imported_,
6050 : // and here we return true.
6051 46 : if (this->is_imported_ && this->is_anonymous())
6052 : {
6053 15 : Type* t = this->typed_identifier_.type();
6054 15 : if (t->points_to() != NULL)
6055 0 : t = t->points_to();
6056 15 : Named_type* nt = t->named_type();
6057 30 : if (nt != NULL
6058 15 : && nt->is_builtin()
6059 30 : && nt->name() == Gogo::unpack_hidden_name(name))
6060 : return true;
6061 : }
6062 :
6063 : return false;
6064 : }
6065 :
6066 : // Return whether this field is an embedded built-in type.
6067 :
6068 : bool
6069 245721 : Struct_field::is_embedded_builtin(Gogo* gogo) const
6070 : {
6071 245721 : const std::string& name(this->field_name());
6072 : // We know that a field is an embedded type if it is anonymous.
6073 : // We can decide if it is a built-in type by checking to see if it is
6074 : // registered globally under the field's name.
6075 : // This allows us to distinguish between embedded built-in types and
6076 : // embedded types that are aliases to built-in types.
6077 245721 : return (this->is_anonymous()
6078 6855 : && !Gogo::is_hidden_name(name)
6079 249977 : && gogo->lookup_global(name.c_str()) != NULL);
6080 : }
6081 :
6082 : // Class Struct_type.
6083 :
6084 : // A hash table used to find identical unnamed structs so that they
6085 : // share method tables.
6086 :
6087 : Struct_type::Identical_structs Struct_type::identical_structs;
6088 :
6089 : // A hash table used to merge method sets for identical unnamed
6090 : // structs.
6091 :
6092 : Struct_type::Struct_method_tables Struct_type::struct_method_tables;
6093 :
6094 : // Message name.
6095 :
6096 : void
6097 25 : Struct_type::do_message_name(std::string* ret) const
6098 : {
6099 25 : if (this->fields_ == NULL || this->fields_->empty())
6100 : {
6101 0 : ret->append("struct{}");
6102 0 : return;
6103 : }
6104 :
6105 25 : ret->append("struct {");
6106 :
6107 25 : bool first = true;
6108 25 : for (Struct_field_list::const_iterator p = this->fields_->begin();
6109 56 : p != this->fields_->end();
6110 31 : ++p)
6111 : {
6112 31 : if (first)
6113 : first = false;
6114 : else
6115 6 : ret->append("; ");
6116 :
6117 31 : if (!p->is_anonymous())
6118 : {
6119 30 : ret->append(p->field_name());
6120 30 : ret->push_back(' ');
6121 : }
6122 :
6123 31 : this->append_message_name(p->type(), ret);
6124 : }
6125 :
6126 25 : ret->append(" }");
6127 : }
6128 :
6129 : // Traversal.
6130 :
6131 : int
6132 57223663 : Struct_type::do_traverse(Traverse* traverse)
6133 : {
6134 57223663 : Struct_field_list* fields = this->fields_;
6135 57223663 : if (fields != NULL)
6136 : {
6137 366184927 : for (Struct_field_list::iterator p = fields->begin();
6138 366184927 : p != fields->end();
6139 308961264 : ++p)
6140 : {
6141 308963214 : if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
6142 57223663 : return TRAVERSE_EXIT;
6143 : }
6144 : }
6145 : return TRAVERSE_CONTINUE;
6146 : }
6147 :
6148 : // Verify that the struct type is complete and valid.
6149 :
6150 : bool
6151 512019 : Struct_type::do_verify(Gogo*)
6152 : {
6153 512019 : Struct_field_list* fields = this->fields_;
6154 512019 : if (fields == NULL)
6155 : return true;
6156 512019 : for (Struct_field_list::iterator p = fields->begin();
6157 3040205 : p != fields->end();
6158 2528186 : ++p)
6159 : {
6160 2528186 : Type* t = p->type();
6161 2528186 : if (p->is_anonymous())
6162 : {
6163 161642 : if ((t->named_type() != NULL && t->points_to() != NULL)
6164 161640 : || (t->named_type() == NULL && t->points_to() != NULL
6165 7666 : && t->points_to()->points_to() != NULL))
6166 : {
6167 2 : go_error_at(p->location(), "embedded type may not be a pointer");
6168 2 : p->set_type(Type::make_error_type());
6169 2 : this->set_is_error();
6170 : }
6171 84652 : else if (t->points_to() != NULL
6172 2535852 : && t->points_to()->interface_type() != NULL)
6173 : {
6174 1 : go_error_at(p->location(),
6175 : "embedded type may not be pointer to interface");
6176 1 : p->set_type(Type::make_error_type());
6177 1 : this->set_is_error();
6178 : }
6179 : }
6180 : }
6181 : return true;
6182 : }
6183 :
6184 : // Whether this contains a pointer.
6185 :
6186 : bool
6187 1021734 : Struct_type::do_has_pointer() const
6188 : {
6189 1021734 : const Struct_field_list* fields = this->fields();
6190 1021734 : if (fields == NULL)
6191 : return false;
6192 1922344 : for (Struct_field_list::const_iterator p = fields->begin();
6193 1922344 : p != fields->end();
6194 900610 : ++p)
6195 : {
6196 1719739 : if (p->type()->has_pointer())
6197 1021734 : return true;
6198 : }
6199 : return false;
6200 : }
6201 :
6202 : // Whether this type is identical to T.
6203 :
6204 : bool
6205 495451 : Struct_type::is_identical(const Struct_type* t, int flags) const
6206 : {
6207 495451 : if (this->is_struct_incomparable_ != t->is_struct_incomparable_)
6208 : return false;
6209 486705 : const Struct_field_list* fields1 = this->fields();
6210 486705 : const Struct_field_list* fields2 = t->fields();
6211 486705 : if (fields1 == NULL || fields2 == NULL)
6212 0 : return fields1 == fields2;
6213 486705 : Struct_field_list::const_iterator pf2 = fields2->begin();
6214 486705 : for (Struct_field_list::const_iterator pf1 = fields1->begin();
6215 1447532 : pf1 != fields1->end();
6216 960827 : ++pf1, ++pf2)
6217 : {
6218 1191461 : if (pf2 == fields2->end())
6219 495451 : return false;
6220 1191461 : if (pf1->field_name() != pf2->field_name())
6221 : return false;
6222 966467 : if (pf1->is_anonymous() != pf2->is_anonymous()
6223 966467 : || !Type::are_identical(pf1->type(), pf2->type(), flags, NULL))
6224 5004 : return false;
6225 961463 : if ((flags & Type::COMPARE_TAGS) != 0)
6226 : {
6227 961323 : if (!pf1->has_tag())
6228 : {
6229 960649 : if (pf2->has_tag())
6230 : return false;
6231 : }
6232 : else
6233 : {
6234 674 : if (!pf2->has_tag())
6235 : return false;
6236 548 : if (pf1->tag() != pf2->tag())
6237 : return false;
6238 : }
6239 : }
6240 : }
6241 256071 : if (pf2 != fields2->end())
6242 : return false;
6243 : return true;
6244 : }
6245 :
6246 : // Whether comparisons of this struct type are simple identity
6247 : // comparisons.
6248 :
6249 : bool
6250 1039369 : Struct_type::do_compare_is_identity(Gogo* gogo)
6251 : {
6252 1039369 : const Struct_field_list* fields = this->fields_;
6253 1039369 : if (fields == NULL)
6254 : return true;
6255 1039369 : int64_t offset = 0;
6256 1039369 : for (Struct_field_list::const_iterator pf = fields->begin();
6257 3644587 : pf != fields->end();
6258 2605218 : ++pf)
6259 : {
6260 2970079 : if (Gogo::is_sink_name(pf->field_name()))
6261 364861 : return false;
6262 :
6263 2956983 : if (!pf->type()->compare_is_identity(gogo))
6264 : return false;
6265 :
6266 2676384 : int64_t field_align;
6267 2676384 : if (!pf->type()->backend_type_align(gogo, &field_align))
6268 : return false;
6269 2676384 : if ((offset & (field_align - 1)) != 0)
6270 : {
6271 : // This struct has padding. We don't guarantee that that
6272 : // padding is zero-initialized for a stack variable, so we
6273 : // can't use memcmp to compare struct values.
6274 : return false;
6275 : }
6276 :
6277 2605218 : int64_t field_size;
6278 2605218 : if (!pf->type()->backend_type_size(gogo, &field_size))
6279 : return false;
6280 2605218 : offset += field_size;
6281 : }
6282 :
6283 674508 : int64_t struct_size;
6284 674508 : if (!this->backend_type_size(gogo, &struct_size))
6285 : return false;
6286 674508 : if (offset != struct_size)
6287 : {
6288 : // Trailing padding may not be zero when on the stack.
6289 : return false;
6290 : }
6291 :
6292 : return true;
6293 : }
6294 :
6295 : // Return whether this struct type is reflexive--whether a value of
6296 : // this type is always equal to itself.
6297 :
6298 : bool
6299 308 : Struct_type::do_is_reflexive()
6300 : {
6301 308 : const Struct_field_list* fields = this->fields_;
6302 308 : if (fields == NULL)
6303 : return true;
6304 1013 : for (Struct_field_list::const_iterator pf = fields->begin();
6305 1013 : pf != fields->end();
6306 705 : ++pf)
6307 : {
6308 734 : if (!pf->type()->is_reflexive())
6309 308 : return false;
6310 : }
6311 : return true;
6312 : }
6313 :
6314 : // Return whether this struct type needs a key update when used as a
6315 : // map key.
6316 :
6317 : bool
6318 308 : Struct_type::do_needs_key_update()
6319 : {
6320 308 : const Struct_field_list* fields = this->fields_;
6321 308 : if (fields == NULL)
6322 : return false;
6323 495 : for (Struct_field_list::const_iterator pf = fields->begin();
6324 495 : pf != fields->end();
6325 187 : ++pf)
6326 : {
6327 437 : if (pf->type()->needs_key_update())
6328 308 : return true;
6329 : }
6330 : return false;
6331 : }
6332 :
6333 : // Return whether computing the hash value of an instance of this
6334 : // struct type might panic.
6335 :
6336 : bool
6337 2 : Struct_type::do_hash_might_panic()
6338 : {
6339 2 : const Struct_field_list* fields = this->fields_;
6340 2 : if (fields == NULL)
6341 : return false;
6342 6 : for (Struct_field_list::const_iterator pf = fields->begin();
6343 6 : pf != fields->end();
6344 4 : ++pf)
6345 : {
6346 4 : if (pf->type()->hash_might_panic())
6347 2 : return true;
6348 : }
6349 : return false;
6350 : }
6351 :
6352 : // Return whether this struct type is permitted to be in the heap.
6353 :
6354 : bool
6355 5684039 : Struct_type::do_in_heap() const
6356 : {
6357 5684039 : const Struct_field_list* fields = this->fields_;
6358 5684039 : if (fields == NULL)
6359 : return true;
6360 39080633 : for (Struct_field_list::const_iterator pf = fields->begin();
6361 39080633 : pf != fields->end();
6362 33396594 : ++pf)
6363 : {
6364 33397035 : if (!pf->type()->in_heap())
6365 5684039 : return false;
6366 : }
6367 : return true;
6368 : }
6369 :
6370 : // Build identity and hash functions for this struct.
6371 :
6372 : // Hash code.
6373 :
6374 : unsigned int
6375 1152769 : Struct_type::do_hash_for_method(Gogo* gogo, int flags) const
6376 : {
6377 1152769 : unsigned int ret = 0;
6378 1152769 : if (this->fields() != NULL)
6379 : {
6380 1152769 : for (Struct_field_list::const_iterator pf = this->fields()->begin();
6381 5336405 : pf != this->fields()->end();
6382 4183636 : ++pf)
6383 4183636 : ret = (ret << 1) + pf->type()->hash_for_method(gogo, flags);
6384 : }
6385 1152769 : ret <<= 2;
6386 1152769 : if (this->is_struct_incomparable_)
6387 463244 : ret <<= 1;
6388 1152769 : return ret;
6389 : }
6390 :
6391 : // Find the local field NAME.
6392 :
6393 : const Struct_field*
6394 1750524 : Struct_type::find_local_field(const std::string& name,
6395 : unsigned int *pindex) const
6396 : {
6397 1750524 : const Struct_field_list* fields = this->fields_;
6398 1750524 : if (fields == NULL)
6399 : return NULL;
6400 1750524 : unsigned int i = 0;
6401 1750524 : for (Struct_field_list::const_iterator pf = fields->begin();
6402 13087791 : pf != fields->end();
6403 11337267 : ++pf, ++i)
6404 : {
6405 11498484 : if (pf->is_field_name(name))
6406 : {
6407 161217 : if (pindex != NULL)
6408 161192 : *pindex = i;
6409 : return &*pf;
6410 : }
6411 : }
6412 : return NULL;
6413 : }
6414 :
6415 : // Return an expression for field NAME in STRUCT_EXPR, or NULL.
6416 :
6417 : Field_reference_expression*
6418 433750 : Struct_type::field_reference(Expression* struct_expr, const std::string& name,
6419 : Location location) const
6420 : {
6421 433750 : unsigned int depth;
6422 433750 : return this->field_reference_depth(struct_expr, name, location, NULL,
6423 433750 : &depth);
6424 : }
6425 :
6426 : // Return an expression for a field, along with the depth at which it
6427 : // was found.
6428 :
6429 : Field_reference_expression*
6430 661984 : Struct_type::field_reference_depth(Expression* struct_expr,
6431 : const std::string& name,
6432 : Location location,
6433 : Saw_named_type* saw,
6434 : unsigned int* depth) const
6435 : {
6436 661984 : const Struct_field_list* fields = this->fields_;
6437 661984 : if (fields == NULL)
6438 : return NULL;
6439 :
6440 : // Look for a field with this name.
6441 661984 : unsigned int i = 0;
6442 661984 : for (Struct_field_list::const_iterator pf = fields->begin();
6443 3277688 : pf != fields->end();
6444 2615704 : ++pf, ++i)
6445 : {
6446 3049491 : if (pf->is_field_name(name))
6447 : {
6448 433787 : *depth = 0;
6449 433787 : return Expression::make_field_reference(struct_expr, i, location);
6450 : }
6451 : }
6452 :
6453 : // Look for an anonymous field which contains a field with this
6454 : // name.
6455 228197 : unsigned int found_depth = 0;
6456 228197 : Field_reference_expression* ret = NULL;
6457 228197 : i = 0;
6458 228197 : for (Struct_field_list::const_iterator pf = fields->begin();
6459 954209 : pf != fields->end();
6460 726012 : ++pf, ++i)
6461 : {
6462 726012 : if (!pf->is_anonymous())
6463 701280 : continue;
6464 :
6465 873096 : Struct_type* st = pf->type()->deref()->struct_type();
6466 436548 : if (st == NULL)
6467 208314 : continue;
6468 :
6469 228234 : Saw_named_type* hold_saw = saw;
6470 228234 : Saw_named_type saw_here;
6471 228234 : Named_type* nt = pf->type()->named_type();
6472 228234 : if (nt == NULL)
6473 212352 : nt = pf->type()->deref()->named_type();
6474 228234 : if (nt != NULL)
6475 : {
6476 : Saw_named_type* q;
6477 1214678 : for (q = saw; q != NULL; q = q->next)
6478 : {
6479 986444 : if (q->nt == nt)
6480 : {
6481 : // If this is an error, it will be reported
6482 : // elsewhere.
6483 : break;
6484 : }
6485 : }
6486 228234 : if (q != NULL)
6487 0 : continue;
6488 228234 : saw_here.next = saw;
6489 228234 : saw_here.nt = nt;
6490 228234 : saw = &saw_here;
6491 : }
6492 :
6493 : // Look for a reference using a NULL struct expression. If we
6494 : // find one, fill in the struct expression with a reference to
6495 : // this field.
6496 228234 : unsigned int subdepth;
6497 228234 : Field_reference_expression* sub = st->field_reference_depth(NULL, name,
6498 : location,
6499 : saw,
6500 : &subdepth);
6501 :
6502 228234 : saw = hold_saw;
6503 :
6504 228234 : if (sub == NULL)
6505 203502 : continue;
6506 :
6507 24732 : if (ret == NULL || subdepth < found_depth)
6508 : {
6509 0 : if (ret != NULL)
6510 0 : delete ret;
6511 24696 : ret = sub;
6512 24696 : found_depth = subdepth;
6513 24696 : Expression* here = Expression::make_field_reference(struct_expr, i,
6514 : location);
6515 24696 : if (pf->type()->points_to() != NULL)
6516 4335 : here = Expression::make_dereference(here,
6517 : Expression::NIL_CHECK_DEFAULT,
6518 : location);
6519 32558 : while (sub->expr() != NULL)
6520 : {
6521 7862 : sub = sub->expr()->deref()->field_reference_expression();
6522 7862 : go_assert(sub != NULL);
6523 : }
6524 24696 : sub->set_struct_expression(here);
6525 24696 : sub->set_implicit(true);
6526 : }
6527 36 : else if (subdepth > found_depth)
6528 35 : delete sub;
6529 : else
6530 : {
6531 : // We do not handle ambiguity here--it should be handled by
6532 : // Type::bind_field_or_method.
6533 1 : delete sub;
6534 1 : found_depth = 0;
6535 1 : ret = NULL;
6536 : }
6537 : }
6538 :
6539 228197 : if (ret != NULL)
6540 24695 : *depth = found_depth + 1;
6541 :
6542 : return ret;
6543 : }
6544 :
6545 : // Return the total number of fields, including embedded fields.
6546 :
6547 : unsigned int
6548 6650 : Struct_type::total_field_count() const
6549 : {
6550 6650 : if (this->fields_ == NULL)
6551 : return 0;
6552 6650 : unsigned int ret = 0;
6553 24995 : for (Struct_field_list::const_iterator pf = this->fields_->begin();
6554 24995 : pf != this->fields_->end();
6555 18345 : ++pf)
6556 : {
6557 18345 : if (!pf->is_anonymous() || pf->type()->struct_type() == NULL)
6558 17872 : ++ret;
6559 : else
6560 946 : ret += pf->type()->struct_type()->total_field_count();
6561 : }
6562 : return ret;
6563 : }
6564 :
6565 : // Return whether NAME is an unexported field, for better error reporting.
6566 :
6567 : bool
6568 18 : Struct_type::is_unexported_local_field(Gogo* gogo,
6569 : const std::string& name) const
6570 : {
6571 18 : const Struct_field_list* fields = this->fields_;
6572 18 : if (fields != NULL)
6573 : {
6574 59 : for (Struct_field_list::const_iterator pf = fields->begin();
6575 59 : pf != fields->end();
6576 41 : ++pf)
6577 49 : if (pf->is_unexported_field_name(gogo, name))
6578 18 : return true;
6579 : }
6580 : return false;
6581 : }
6582 :
6583 : // Finalize the methods of an unnamed struct.
6584 :
6585 : void
6586 119380 : Struct_type::finalize_methods(Gogo* gogo)
6587 : {
6588 119380 : if (this->all_methods_ != NULL)
6589 98059 : return;
6590 :
6591 : // It is possible to have multiple identical structs that have
6592 : // methods. We want them to share method tables. Otherwise we will
6593 : // emit identical methods more than once, which is bad since they
6594 : // will even have the same names.
6595 92182 : std::pair<Identical_structs::iterator, bool> ins =
6596 92182 : Struct_type::identical_structs.insert(std::make_pair(this, this));
6597 92182 : if (!ins.second)
6598 : {
6599 : // An identical struct was already entered into the hash table.
6600 : // Note that finalize_methods is, fortunately, not recursive.
6601 70861 : this->all_methods_ = ins.first->second->all_methods_;
6602 70861 : return;
6603 : }
6604 :
6605 21321 : Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
6606 : }
6607 :
6608 : // Return the method NAME, or NULL if there isn't one or if it is
6609 : // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
6610 : // ambiguous.
6611 :
6612 : Method*
6613 67120 : Struct_type::method_function(const std::string& name, bool* is_ambiguous) const
6614 : {
6615 67120 : return Type::method_function(this->all_methods_, name, is_ambiguous);
6616 : }
6617 :
6618 : // Return a pointer to the interface method table for this type for
6619 : // the interface INTERFACE. IS_POINTER is true if this is for a
6620 : // pointer to THIS.
6621 :
6622 : Expression*
6623 622 : Struct_type::interface_method_table(Interface_type* interface,
6624 : bool is_pointer)
6625 : {
6626 622 : std::pair<Struct_type*, Struct_type::Struct_method_table_pair*>
6627 622 : val(this, nullptr);
6628 622 : std::pair<Struct_type::Struct_method_tables::iterator, bool> ins =
6629 622 : Struct_type::struct_method_tables.insert(val);
6630 :
6631 622 : Struct_method_table_pair* smtp;
6632 622 : if (!ins.second)
6633 541 : smtp = ins.first->second;
6634 : else
6635 : {
6636 81 : smtp = new Struct_method_table_pair();
6637 81 : smtp->first = NULL;
6638 81 : smtp->second = NULL;
6639 81 : ins.first->second = smtp;
6640 : }
6641 :
6642 622 : return Type::interface_method_table(this, interface, is_pointer,
6643 622 : &smtp->first, &smtp->second);
6644 : }
6645 :
6646 : // Convert struct fields to the backend representation. This is not
6647 : // declared in types.h so that types.h doesn't have to #include
6648 : // backend.h.
6649 :
6650 : static void
6651 779115 : get_backend_struct_fields(Gogo* gogo, Struct_type* type, bool use_placeholder,
6652 : std::vector<Backend::Btyped_identifier>* bfields)
6653 : {
6654 779115 : const Struct_field_list* fields = type->fields();
6655 779115 : bfields->resize(fields->size());
6656 779115 : size_t i = 0;
6657 779115 : int64_t lastsize = 0;
6658 779115 : bool saw_nonzero = false;
6659 779115 : for (Struct_field_list::const_iterator p = fields->begin();
6660 4343279 : p != fields->end();
6661 3564164 : ++p, ++i)
6662 : {
6663 3564164 : (*bfields)[i].name = Gogo::unpack_hidden_name(p->field_name());
6664 3564164 : (*bfields)[i].btype = (use_placeholder
6665 3564164 : ? p->type()->get_backend_placeholder(gogo)
6666 965415 : : p->type()->get_backend(gogo));
6667 3564164 : (*bfields)[i].location = p->location();
6668 3564164 : int64_t size = gogo->backend()->type_size((*bfields)[i].btype);
6669 3564164 : if (size != 0)
6670 3539989 : saw_nonzero = true;
6671 :
6672 3564164 : if (size > 0 || !Gogo::is_sink_name(p->field_name()))
6673 : lastsize = size;
6674 : else
6675 : {
6676 : // There is an unreferenceable field of zero size. This
6677 : // doesn't affect whether we may need zero padding, so leave
6678 : // lastsize unchanged.
6679 : }
6680 : }
6681 779115 : go_assert(i == fields->size());
6682 779115 : if (saw_nonzero && lastsize == 0 && !type->is_results_struct())
6683 : {
6684 : // For nonzero-sized structs which end in a zero-sized thing, we add
6685 : // an extra byte of padding to the type. This padding ensures that
6686 : // taking the address of the zero-sized thing can't manufacture a
6687 : // pointer to the next object in the heap. See issue 9401.
6688 1197 : size_t n = fields->size();
6689 1197 : bfields->resize(n + 1);
6690 1197 : (*bfields)[n].name = "_";
6691 1197 : (*bfields)[n].btype = Type::lookup_integer_type("uint8")->get_backend(gogo);
6692 1197 : (*bfields)[n].location = (*bfields)[n-1].location;
6693 1197 : type->set_has_padding();
6694 : }
6695 779115 : }
6696 :
6697 : // Get the backend representation for a struct type.
6698 :
6699 : Btype*
6700 287071 : Struct_type::do_get_backend(Gogo* gogo)
6701 : {
6702 287071 : std::vector<Backend::Btyped_identifier> bfields;
6703 287071 : get_backend_struct_fields(gogo, this, false, &bfields);
6704 287071 : return gogo->backend()->struct_type(bfields);
6705 287071 : }
6706 :
6707 : // Finish the backend representation of the fields of a struct.
6708 :
6709 : void
6710 359666 : Struct_type::finish_backend_fields(Gogo* gogo)
6711 : {
6712 359666 : const Struct_field_list* fields = this->fields_;
6713 359666 : if (fields != NULL)
6714 : {
6715 2134451 : for (Struct_field_list::const_iterator p = fields->begin();
6716 2134451 : p != fields->end();
6717 1774785 : ++p)
6718 1774785 : p->type()->get_backend(gogo);
6719 : }
6720 359666 : }
6721 :
6722 : // The type of a struct type descriptor.
6723 :
6724 : Type*
6725 51042 : Struct_type::make_struct_type_descriptor_type()
6726 : {
6727 51042 : static Type* ret;
6728 51042 : if (ret == NULL)
6729 : {
6730 4646 : Type* tdt = Type::make_type_descriptor_type();
6731 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
6732 :
6733 4646 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
6734 4646 : Type* string_type = Type::lookup_string_type();
6735 4646 : Type* pointer_string_type = Type::make_pointer_type(string_type);
6736 :
6737 4646 : Struct_type* sf =
6738 4646 : Type::make_builtin_struct_type(5,
6739 : "name", pointer_string_type,
6740 : "pkgPath", pointer_string_type,
6741 : "typ", ptdt,
6742 : "tag", pointer_string_type,
6743 : "offsetAnon", uintptr_type);
6744 4646 : Type* nsf = Type::make_builtin_named_type("structField", sf);
6745 :
6746 4646 : Type* slice_type = Type::make_array_type(nsf, NULL);
6747 :
6748 4646 : Struct_type* s = Type::make_builtin_struct_type(2,
6749 : "", tdt,
6750 : "fields", slice_type);
6751 :
6752 4646 : ret = Type::make_builtin_named_type("StructType", s);
6753 : }
6754 :
6755 51042 : return ret;
6756 : }
6757 :
6758 : // Build a type descriptor for a struct type.
6759 :
6760 : Expression*
6761 46396 : Struct_type::do_type_descriptor(Gogo* gogo, Named_type* name)
6762 : {
6763 46396 : Location bloc = Linemap::predeclared_location();
6764 :
6765 46396 : Type* stdt = Struct_type::make_struct_type_descriptor_type();
6766 :
6767 92792 : const Struct_field_list* fields = stdt->struct_type()->fields();
6768 :
6769 46396 : Expression_list* vals = new Expression_list();
6770 46396 : vals->reserve(2);
6771 :
6772 46396 : const Methods* methods = this->methods();
6773 : // A named struct should not have methods--the methods should attach
6774 : // to the named type.
6775 46396 : go_assert(methods == NULL || name == NULL);
6776 :
6777 46396 : Struct_field_list::const_iterator ps = fields->begin();
6778 46396 : go_assert(ps->is_field_name("_type"));
6779 46396 : vals->push_back(this->type_descriptor_constructor(gogo,
6780 : RUNTIME_TYPE_KIND_STRUCT,
6781 : name, methods, true));
6782 :
6783 46396 : ++ps;
6784 46396 : go_assert(ps->is_field_name("fields"));
6785 :
6786 46396 : Expression_list* elements = new Expression_list();
6787 46396 : elements->reserve(this->fields_->size());
6788 92792 : Type* element_type = ps->type()->array_type()->element_type();
6789 46396 : for (Struct_field_list::const_iterator pf = this->fields_->begin();
6790 231385 : pf != this->fields_->end();
6791 184989 : ++pf)
6792 : {
6793 369978 : const Struct_field_list* f = element_type->struct_type()->fields();
6794 :
6795 184989 : Expression_list* fvals = new Expression_list();
6796 184989 : fvals->reserve(5);
6797 :
6798 184989 : Struct_field_list::const_iterator q = f->begin();
6799 184989 : go_assert(q->is_field_name("name"));
6800 184989 : std::string n = Gogo::unpack_hidden_name(pf->field_name());
6801 184989 : Expression* s = Expression::make_string(n, bloc);
6802 184989 : fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6803 :
6804 184989 : ++q;
6805 184989 : go_assert(q->is_field_name("pkgPath"));
6806 184989 : bool is_embedded_builtin = pf->is_embedded_builtin(gogo);
6807 184989 : if (!Gogo::is_hidden_name(pf->field_name()) && !is_embedded_builtin)
6808 75918 : fvals->push_back(Expression::make_nil(bloc));
6809 : else
6810 : {
6811 109071 : if (is_embedded_builtin)
6812 142 : n = gogo->package_name();
6813 : else
6814 108929 : n = Gogo::hidden_name_pkgpath(pf->field_name());
6815 109071 : s = Expression::make_string(n, bloc);
6816 109071 : fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6817 : }
6818 :
6819 184989 : ++q;
6820 184989 : go_assert(q->is_field_name("typ"));
6821 184989 : fvals->push_back(Expression::make_type_descriptor(pf->type(), bloc));
6822 :
6823 184989 : ++q;
6824 184989 : go_assert(q->is_field_name("tag"));
6825 184989 : if (!pf->has_tag())
6826 183030 : fvals->push_back(Expression::make_nil(bloc));
6827 : else
6828 : {
6829 1959 : s = Expression::make_string(pf->tag(), bloc);
6830 1959 : fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
6831 : }
6832 :
6833 184989 : ++q;
6834 184989 : go_assert(q->is_field_name("offsetAnon"));
6835 184989 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
6836 184989 : Expression* o = Expression::make_struct_field_offset(this, &*pf);
6837 184989 : Expression* one = Expression::make_integer_ul(1, uintptr_type, bloc);
6838 184989 : o = Expression::make_binary(OPERATOR_LSHIFT, o, one, bloc);
6839 184989 : int av = pf->is_anonymous() ? 1 : 0;
6840 184989 : Expression* anon = Expression::make_integer_ul(av, uintptr_type, bloc);
6841 184989 : o = Expression::make_binary(OPERATOR_OR, o, anon, bloc);
6842 184989 : fvals->push_back(o);
6843 :
6844 184989 : Expression* v = Expression::make_struct_composite_literal(element_type,
6845 : fvals, bloc);
6846 184989 : elements->push_back(v);
6847 184989 : }
6848 :
6849 46396 : vals->push_back(Expression::make_slice_composite_literal(ps->type(),
6850 : elements, bloc));
6851 :
6852 46396 : return Expression::make_struct_composite_literal(stdt, vals, bloc);
6853 : }
6854 :
6855 : // Write the hash function for a struct which can not use the identity
6856 : // function.
6857 :
6858 : void
6859 154 : Struct_type::write_hash_function(Gogo* gogo, Named_object* function,
6860 : Function_type* hash_fntype)
6861 : {
6862 154 : Location bloc = Linemap::predeclared_location();
6863 :
6864 : // The pointer to the struct that we are going to hash. This is an
6865 : // argument to the hash function we are implementing here.
6866 154 : Named_object* key_arg = gogo->lookup("key", NULL);
6867 154 : go_assert(key_arg != NULL);
6868 154 : Type* key_arg_type = key_arg->var_value()->type();
6869 :
6870 : // The seed argument to the hash function.
6871 154 : Named_object* seed_arg = gogo->lookup("seed", NULL);
6872 154 : go_assert(seed_arg != NULL);
6873 :
6874 154 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
6875 :
6876 : // Make a temporary to hold the return value, initialized to the seed.
6877 154 : Expression* ref = Expression::make_var_reference(seed_arg, bloc);
6878 154 : Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
6879 : bloc);
6880 154 : retval->determine_types(gogo);
6881 154 : gogo->add_statement(retval);
6882 :
6883 : // Make a temporary to hold the key as a uintptr.
6884 154 : ref = Expression::make_var_reference(key_arg, bloc);
6885 154 : ref = Expression::make_cast(uintptr_type, ref, bloc);
6886 154 : Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
6887 : bloc);
6888 154 : key->determine_types(gogo);
6889 154 : gogo->add_statement(key);
6890 :
6891 : // Loop over the struct fields.
6892 154 : const Struct_field_list* fields = this->fields_;
6893 587 : for (Struct_field_list::const_iterator pf = fields->begin();
6894 587 : pf != fields->end();
6895 433 : ++pf)
6896 : {
6897 433 : if (Gogo::is_sink_name(pf->field_name()))
6898 12 : continue;
6899 :
6900 : // Get a pointer to the value of this field.
6901 421 : Expression* offset = Expression::make_struct_field_offset(this, &*pf);
6902 421 : ref = Expression::make_temporary_reference(key, bloc);
6903 421 : Expression* subkey = Expression::make_binary(OPERATOR_PLUS, ref, offset,
6904 : bloc);
6905 421 : subkey = Expression::make_cast(key_arg_type, subkey, bloc);
6906 :
6907 : // Get the hash function to use for the type of this field.
6908 421 : Named_object* hash_fn =
6909 421 : pf->type()->unalias()->hash_function(gogo, hash_fntype);
6910 :
6911 : // Call the hash function for the field, passing retval as the seed.
6912 421 : ref = Expression::make_temporary_reference(retval, bloc);
6913 421 : Expression_list* args = new Expression_list();
6914 421 : args->push_back(subkey);
6915 421 : args->push_back(ref);
6916 421 : Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
6917 421 : Expression* call = Expression::make_call(func, args, false, bloc);
6918 :
6919 : // Set retval to the result.
6920 421 : Temporary_reference_expression* tref =
6921 421 : Expression::make_temporary_reference(retval, bloc);
6922 421 : tref->set_is_lvalue();
6923 421 : Statement* s = Statement::make_assignment(tref, call, bloc);
6924 421 : s->determine_types(gogo);
6925 421 : gogo->add_statement(s);
6926 : }
6927 :
6928 : // Return retval to the caller of the hash function.
6929 154 : Expression_list* vals = new Expression_list();
6930 154 : ref = Expression::make_temporary_reference(retval, bloc);
6931 154 : vals->push_back(ref);
6932 154 : Statement* s = Statement::make_return_statement(function, vals, bloc);
6933 154 : s->determine_types(gogo);
6934 154 : gogo->add_statement(s);
6935 154 : }
6936 :
6937 : // Write the equality function for a struct which can not use the
6938 : // identity function.
6939 :
6940 : void
6941 12337 : Struct_type::write_equal_function(Gogo* gogo, Named_object* function,
6942 : Named_type* name)
6943 : {
6944 12337 : Location bloc = Linemap::predeclared_location();
6945 :
6946 : // The pointers to the structs we are going to compare.
6947 12337 : Named_object* key1_arg = gogo->lookup("key1", NULL);
6948 12337 : Named_object* key2_arg = gogo->lookup("key2", NULL);
6949 12337 : go_assert(key1_arg != NULL && key2_arg != NULL);
6950 :
6951 : // Build temporaries with the right types.
6952 12337 : Type* pt = Type::make_pointer_type(name != NULL
6953 : ? static_cast<Type*>(name)
6954 : : static_cast<Type*>(this));
6955 :
6956 12337 : Expression* ref = Expression::make_var_reference(key1_arg, bloc);
6957 12337 : ref = Expression::make_unsafe_cast(pt, ref, bloc);
6958 12337 : Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
6959 12337 : p1->determine_types(gogo);
6960 12337 : gogo->add_statement(p1);
6961 :
6962 12337 : ref = Expression::make_var_reference(key2_arg, bloc);
6963 12337 : ref = Expression::make_unsafe_cast(pt, ref, bloc);
6964 12337 : Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
6965 12337 : p2->determine_types(gogo);
6966 12337 : gogo->add_statement(p2);
6967 :
6968 12337 : const Struct_field_list* fields = this->fields_;
6969 12337 : unsigned int field_index = 0;
6970 12337 : for (Struct_field_list::const_iterator pf = fields->begin();
6971 52799 : pf != fields->end();
6972 40462 : ++pf, ++field_index)
6973 : {
6974 40462 : if (Gogo::is_sink_name(pf->field_name()))
6975 758 : continue;
6976 :
6977 : // Compare one field in both P1 and P2.
6978 39704 : Expression* f1 = Expression::make_temporary_reference(p1, bloc);
6979 39704 : f1 = Expression::make_dereference(f1, Expression::NIL_CHECK_DEFAULT,
6980 : bloc);
6981 39704 : f1 = Expression::make_field_reference(f1, field_index, bloc);
6982 :
6983 39704 : Expression* f2 = Expression::make_temporary_reference(p2, bloc);
6984 39704 : f2 = Expression::make_dereference(f2, Expression::NIL_CHECK_DEFAULT,
6985 : bloc);
6986 39704 : f2 = Expression::make_field_reference(f2, field_index, bloc);
6987 :
6988 39704 : Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, f1, f2, bloc);
6989 :
6990 : // If the values are not equal, return false.
6991 39704 : gogo->start_block(bloc);
6992 39704 : Expression_list* vals = new Expression_list();
6993 39704 : vals->push_back(Expression::make_boolean(false, bloc));
6994 39704 : Statement* s = Statement::make_return_statement(function, vals, bloc);
6995 39704 : s->determine_types(gogo);
6996 39704 : gogo->add_statement(s);
6997 39704 : Block* then_block = gogo->finish_block(bloc);
6998 :
6999 39704 : s = Statement::make_if_statement(cond, then_block, NULL, bloc);
7000 39704 : s->determine_types(gogo);
7001 39704 : gogo->add_statement(s);
7002 : }
7003 :
7004 : // All the fields are equal, so return true.
7005 12337 : Expression_list* vals = new Expression_list();
7006 12337 : vals->push_back(Expression::make_boolean(true, bloc));
7007 12337 : Statement* s = Statement::make_return_statement(function, vals, bloc);
7008 12337 : s->determine_types(gogo);
7009 12337 : gogo->add_statement(s);
7010 12337 : }
7011 :
7012 : // Reflection string.
7013 :
7014 : void
7015 16697 : Struct_type::do_reflection(Gogo* gogo, std::string* ret) const
7016 : {
7017 16697 : ret->append("struct {");
7018 :
7019 64056 : for (Struct_field_list::const_iterator p = this->fields_->begin();
7020 64056 : p != this->fields_->end();
7021 47359 : ++p)
7022 : {
7023 47359 : if (p != this->fields_->begin())
7024 32127 : ret->push_back(';');
7025 47359 : ret->push_back(' ');
7026 47359 : if (!p->is_anonymous())
7027 : {
7028 92506 : ret->append(Gogo::unpack_hidden_name(p->field_name()));
7029 46253 : ret->push_back(' ');
7030 : }
7031 47359 : if (p->is_anonymous()
7032 1106 : && p->type()->named_type() != NULL
7033 48375 : && p->type()->named_type()->is_alias())
7034 0 : p->type()->named_type()->append_reflection_type_name(gogo, true, ret);
7035 : else
7036 47359 : this->append_reflection(p->type(), gogo, ret);
7037 :
7038 47359 : if (p->has_tag())
7039 : {
7040 250 : const std::string& tag(p->tag());
7041 250 : ret->append(" \"");
7042 3913 : for (std::string::const_iterator pt = tag.begin();
7043 3913 : pt != tag.end();
7044 3663 : ++pt)
7045 : {
7046 3663 : if (*pt == '\0')
7047 0 : ret->append("\\x00");
7048 3663 : else if (*pt == '\n')
7049 0 : ret->append("\\n");
7050 3663 : else if (*pt == '\t')
7051 0 : ret->append("\\t");
7052 3663 : else if (*pt == '"')
7053 436 : ret->append("\\\"");
7054 3227 : else if (*pt == '\\')
7055 52 : ret->append("\\\\");
7056 : else
7057 3175 : ret->push_back(*pt);
7058 : }
7059 250 : ret->push_back('"');
7060 : }
7061 : }
7062 :
7063 16697 : if (!this->fields_->empty())
7064 15232 : ret->push_back(' ');
7065 :
7066 16697 : ret->push_back('}');
7067 16697 : }
7068 :
7069 : // If the offset of field INDEX in the backend implementation can be
7070 : // determined, set *POFFSET to the offset in bytes and return true.
7071 : // Otherwise, return false.
7072 :
7073 : bool
7074 611 : Struct_type::backend_field_offset(Gogo* gogo, unsigned int index,
7075 : int64_t* poffset)
7076 : {
7077 611 : if (!this->is_backend_type_size_known(gogo))
7078 : return false;
7079 611 : Btype* bt = this->get_backend_placeholder(gogo);
7080 611 : *poffset = gogo->backend()->type_field_offset(bt, index);
7081 611 : return true;
7082 : }
7083 :
7084 : // Export.
7085 :
7086 : void
7087 44541 : Struct_type::do_export(Export* exp) const
7088 : {
7089 44541 : exp->write_c_string("struct { ");
7090 44541 : const Struct_field_list* fields = this->fields_;
7091 44541 : go_assert(fields != NULL);
7092 278353 : for (Struct_field_list::const_iterator p = fields->begin();
7093 278353 : p != fields->end();
7094 233812 : ++p)
7095 : {
7096 233812 : if (p->is_anonymous())
7097 5428 : exp->write_string("? ");
7098 : else
7099 : {
7100 228384 : exp->write_string(p->field_name());
7101 228384 : exp->write_c_string(" ");
7102 : }
7103 233812 : exp->write_type(p->type());
7104 :
7105 233812 : if (p->has_tag())
7106 : {
7107 3407 : exp->write_c_string(" ");
7108 3407 : Expression* expr =
7109 3407 : Expression::make_string(p->tag(), Linemap::predeclared_location());
7110 :
7111 3407 : Export_function_body efb(exp, 0);
7112 3407 : expr->export_expression(&efb);
7113 3407 : exp->write_string(efb.body());
7114 :
7115 3407 : delete expr;
7116 3407 : }
7117 :
7118 233812 : exp->write_c_string("; ");
7119 : }
7120 44541 : exp->write_c_string("}");
7121 44541 : }
7122 :
7123 : // Import.
7124 :
7125 : Struct_type*
7126 383011 : Struct_type::do_import(Import* imp)
7127 : {
7128 383011 : imp->require_c_string("struct { ");
7129 383011 : Struct_field_list* fields = new Struct_field_list;
7130 383011 : if (imp->peek_char() != '}')
7131 : {
7132 4033584 : while (true)
7133 : {
7134 2204384 : std::string name;
7135 2204384 : if (imp->match_c_string("? "))
7136 39210 : imp->advance(2);
7137 : else
7138 : {
7139 2165174 : name = imp->read_identifier();
7140 2165174 : imp->require_c_string(" ");
7141 : }
7142 2204384 : Type* ftype = imp->read_type();
7143 :
7144 4408768 : Struct_field sf(Typed_identifier(name, ftype, imp->location()));
7145 2204384 : sf.set_is_imported();
7146 :
7147 2204384 : if (imp->peek_char() == ' ')
7148 : {
7149 9760 : imp->advance(1);
7150 9760 : Expression* expr = Expression::import_expression(imp,
7151 9760 : imp->location());
7152 9760 : String_expression* sexpr = expr->string_expression();
7153 0 : go_assert(sexpr != NULL);
7154 9760 : sf.set_tag(sexpr->val());
7155 9760 : delete sexpr;
7156 : }
7157 :
7158 2204384 : imp->require_c_string("; ");
7159 2204384 : fields->push_back(sf);
7160 2204384 : if (imp->peek_char() == '}')
7161 : break;
7162 2204384 : }
7163 : }
7164 383011 : imp->require_c_string("}");
7165 :
7166 383011 : return Type::make_struct_type(fields, imp->location());
7167 : }
7168 :
7169 : // Whether we can write this struct type to a C header file.
7170 : // We can't if any of the fields are structs defined in a different package.
7171 :
7172 : bool
7173 2720 : Struct_type::can_write_to_c_header(
7174 : std::vector<const Named_object*>* needs,
7175 : std::vector<const Named_object*>* declare) const
7176 : {
7177 2720 : const Struct_field_list* fields = this->fields_;
7178 2720 : if (fields == NULL || fields->empty())
7179 : return false;
7180 : int sinks = 0;
7181 20384 : for (Struct_field_list::const_iterator p = fields->begin();
7182 23060 : p != fields->end();
7183 20384 : ++p)
7184 : {
7185 20452 : if (!this->can_write_type_to_c_header(p->type(), needs, declare))
7186 112 : return false;
7187 20384 : if (Gogo::message_name(p->field_name()) == "_")
7188 84 : sinks++;
7189 : }
7190 2608 : if (sinks > 1)
7191 : return false;
7192 : return true;
7193 : }
7194 :
7195 : // Whether we can write the type T to a C header file.
7196 :
7197 : bool
7198 21060 : Struct_type::can_write_type_to_c_header(
7199 : const Type* t,
7200 : std::vector<const Named_object*>* needs,
7201 : std::vector<const Named_object*>* declare) const
7202 : {
7203 35612 : t = t->forwarded();
7204 35612 : switch (t->classification())
7205 : {
7206 : case TYPE_ERROR:
7207 : case TYPE_FORWARD:
7208 : return false;
7209 :
7210 : case TYPE_VOID:
7211 : case TYPE_BOOLEAN:
7212 : case TYPE_INTEGER:
7213 : case TYPE_FLOAT:
7214 : case TYPE_COMPLEX:
7215 : case TYPE_STRING:
7216 : case TYPE_FUNCTION:
7217 : case TYPE_MAP:
7218 : case TYPE_CHANNEL:
7219 : case TYPE_INTERFACE:
7220 : return true;
7221 :
7222 4348 : case TYPE_POINTER:
7223 : // Don't try to handle a pointer to an array.
7224 4348 : if (t->points_to()->array_type() != NULL
7225 116 : && !t->points_to()->is_slice_type())
7226 : return false;
7227 :
7228 4324 : if (t->points_to()->named_type() != NULL
7229 4324 : && t->points_to()->struct_type() != NULL)
7230 3012 : declare->push_back(t->points_to()->named_type()->named_object());
7231 : return true;
7232 :
7233 40 : case TYPE_STRUCT:
7234 80 : return t->struct_type()->can_write_to_c_header(needs, declare);
7235 :
7236 1232 : case TYPE_ARRAY:
7237 1232 : if (t->is_slice_type())
7238 : return true;
7239 1896 : return this->can_write_type_to_c_header(t->array_type()->element_type(),
7240 948 : needs, declare);
7241 :
7242 16072 : case TYPE_NAMED:
7243 16072 : {
7244 16072 : const Named_object* no = t->named_type()->named_object();
7245 16072 : if (no->package() != NULL)
7246 : {
7247 732 : if (t->is_unsafe_pointer_type())
7248 : return true;
7249 : return false;
7250 : }
7251 15340 : if (t->struct_type() != NULL)
7252 : {
7253 : // We will accept empty struct fields, but not print them.
7254 3472 : if (t->struct_type()->total_field_count() == 0)
7255 : return true;
7256 1456 : needs->push_back(no);
7257 2912 : return t->struct_type()->can_write_to_c_header(needs, declare);
7258 : }
7259 13604 : return this->can_write_type_to_c_header(t->base(), needs, declare);
7260 : }
7261 :
7262 0 : case TYPE_CALL_MULTIPLE_RESULT:
7263 0 : case TYPE_NIL:
7264 0 : case TYPE_SINK:
7265 0 : default:
7266 0 : go_unreachable();
7267 : }
7268 : }
7269 :
7270 : // Write this struct to a C header file.
7271 :
7272 : void
7273 620 : Struct_type::write_to_c_header(std::ostream& os) const
7274 : {
7275 620 : const Struct_field_list* fields = this->fields_;
7276 4024 : for (Struct_field_list::const_iterator p = fields->begin();
7277 4024 : p != fields->end();
7278 3404 : ++p)
7279 : {
7280 : // Skip fields that are empty struct types. The C code can't
7281 : // refer to them anyhow.
7282 3404 : if (p->type()->struct_type() != NULL
7283 584 : && p->type()->struct_type()->total_field_count() == 0)
7284 8 : continue;
7285 :
7286 3396 : os << '\t';
7287 3396 : this->write_field_to_c_header(os, p->field_name(), p->type());
7288 3396 : os << ';' << std::endl;
7289 : }
7290 620 : }
7291 :
7292 : // Write the type of a struct field to a C header file.
7293 :
7294 : void
7295 4228 : Struct_type::write_field_to_c_header(std::ostream& os, const std::string& name,
7296 : const Type *t) const
7297 : {
7298 6172 : bool print_name = true;
7299 6172 : t = t->forwarded();
7300 6172 : switch (t->classification())
7301 : {
7302 0 : case TYPE_VOID:
7303 0 : os << "void";
7304 0 : break;
7305 :
7306 236 : case TYPE_BOOLEAN:
7307 236 : os << "_Bool";
7308 236 : break;
7309 :
7310 1560 : case TYPE_INTEGER:
7311 1560 : {
7312 1560 : const Integer_type* it = t->integer_type();
7313 1560 : if (it->is_unsigned())
7314 1076 : os << 'u';
7315 1560 : os << "int" << it->bits() << "_t";
7316 : }
7317 1560 : break;
7318 :
7319 36 : case TYPE_FLOAT:
7320 72 : switch (t->float_type()->bits())
7321 : {
7322 0 : case 32:
7323 0 : os << "float";
7324 0 : break;
7325 36 : case 64:
7326 36 : os << "double";
7327 36 : break;
7328 0 : default:
7329 0 : go_unreachable();
7330 : }
7331 : break;
7332 :
7333 0 : case TYPE_COMPLEX:
7334 0 : switch (t->complex_type()->bits())
7335 : {
7336 0 : case 64:
7337 0 : os << "float _Complex";
7338 0 : break;
7339 0 : case 128:
7340 0 : os << "double _Complex";
7341 0 : break;
7342 0 : default:
7343 0 : go_unreachable();
7344 : }
7345 : break;
7346 :
7347 84 : case TYPE_STRING:
7348 84 : os << "String";
7349 84 : break;
7350 :
7351 36 : case TYPE_FUNCTION:
7352 36 : os << "FuncVal*";
7353 36 : break;
7354 :
7355 608 : case TYPE_POINTER:
7356 608 : {
7357 608 : std::vector<const Named_object*> needs;
7358 608 : std::vector<const Named_object*> declare;
7359 608 : if (!this->can_write_type_to_c_header(t->points_to(), &needs,
7360 : &declare))
7361 4 : os << "void*";
7362 : else
7363 : {
7364 604 : this->write_field_to_c_header(os, "", t->points_to());
7365 604 : os << '*';
7366 : }
7367 608 : }
7368 608 : break;
7369 :
7370 0 : case TYPE_MAP:
7371 0 : os << "Map*";
7372 0 : break;
7373 :
7374 0 : case TYPE_CHANNEL:
7375 0 : os << "Chan*";
7376 0 : break;
7377 :
7378 8 : case TYPE_INTERFACE:
7379 16 : if (t->interface_type()->is_empty())
7380 8 : os << "Eface";
7381 : else
7382 0 : os << "Iface";
7383 : break;
7384 :
7385 24 : case TYPE_STRUCT:
7386 24 : os << "struct {" << std::endl;
7387 48 : t->struct_type()->write_to_c_header(os);
7388 24 : os << "\t}";
7389 24 : break;
7390 :
7391 324 : case TYPE_ARRAY:
7392 324 : if (t->is_slice_type())
7393 96 : os << "Slice";
7394 : else
7395 : {
7396 228 : const Type *ele = t;
7397 228 : std::vector<const Type*> array_types;
7398 684 : while (ele->array_type() != NULL && !ele->is_slice_type())
7399 : {
7400 228 : array_types.push_back(ele);
7401 456 : ele = ele->array_type()->element_type();
7402 : }
7403 228 : this->write_field_to_c_header(os, "", ele);
7404 456 : os << ' ' << Gogo::message_name(name);
7405 228 : print_name = false;
7406 456 : while (!array_types.empty())
7407 : {
7408 228 : ele = array_types.back();
7409 228 : array_types.pop_back();
7410 228 : os << '[';
7411 228 : Numeric_constant nc;
7412 456 : if (!ele->array_type()->length()->numeric_constant_value(&nc))
7413 0 : go_unreachable();
7414 228 : mpz_t val;
7415 228 : if (!nc.to_int(&val))
7416 0 : go_unreachable();
7417 228 : char* s = mpz_get_str(NULL, 10, val);
7418 228 : os << s;
7419 228 : free(s);
7420 228 : mpz_clear(val);
7421 228 : os << ']';
7422 228 : }
7423 228 : }
7424 : break;
7425 :
7426 3256 : case TYPE_NAMED:
7427 3256 : {
7428 3256 : const Named_object* no = t->named_type()->named_object();
7429 3256 : if (t->struct_type() != NULL)
7430 1480 : os << "struct " << no->message_name();
7431 2516 : else if (t->is_unsafe_pointer_type())
7432 132 : os << "void*";
7433 2384 : else if (t == Type::lookup_integer_type("uintptr"))
7434 440 : os << "uintptr_t";
7435 : else
7436 : {
7437 1944 : this->write_field_to_c_header(os, name, t->base());
7438 : print_name = false;
7439 : }
7440 : }
7441 : break;
7442 :
7443 0 : case TYPE_ERROR:
7444 0 : case TYPE_FORWARD:
7445 0 : case TYPE_CALL_MULTIPLE_RESULT:
7446 0 : case TYPE_NIL:
7447 0 : case TYPE_SINK:
7448 0 : default:
7449 0 : go_unreachable();
7450 : }
7451 :
7452 4228 : if (print_name && !name.empty())
7453 6336 : os << ' ' << Gogo::message_name(name);
7454 4228 : }
7455 :
7456 : // Make a struct type.
7457 :
7458 : Struct_type*
7459 737398 : Type::make_struct_type(Struct_field_list* fields,
7460 : Location location)
7461 : {
7462 737398 : return new Struct_type(fields, location);
7463 : }
7464 :
7465 : // Class Array_type.
7466 :
7467 : // Store the length of an array as an int64_t into *PLEN. Return
7468 : // false if the length can not be determined. This will assert if
7469 : // called for a slice.
7470 :
7471 : bool
7472 145711 : Array_type::int_length(int64_t* plen) const
7473 : {
7474 145711 : go_assert(this->length_ != NULL);
7475 145711 : Numeric_constant nc;
7476 145711 : if (!this->length_->numeric_constant_value(&nc))
7477 : return false;
7478 145703 : return nc.to_memory_size(plen);
7479 145711 : }
7480 :
7481 : // Whether two array types are identical.
7482 :
7483 : bool
7484 13923609 : Array_type::is_identical(const Array_type* t, int flags) const
7485 : {
7486 13923609 : if (!Type::are_identical(this->element_type(), t->element_type(),
7487 : flags, NULL))
7488 : return false;
7489 :
7490 13910857 : if (this->is_array_incomparable_ != t->is_array_incomparable_)
7491 : return false;
7492 :
7493 13910857 : Expression* l1 = this->length();
7494 13910857 : Expression* l2 = t->length();
7495 :
7496 : // Slices of the same element type are identical.
7497 13910857 : if (l1 == NULL && l2 == NULL)
7498 : return true;
7499 :
7500 : // Arrays of the same element type are identical if they have the
7501 : // same length.
7502 12800720 : if (l1 != NULL && l2 != NULL)
7503 : {
7504 11316523 : if (l1 == l2)
7505 : return true;
7506 :
7507 : // Try to determine the lengths. If we can't, assume the arrays
7508 : // are not identical.
7509 11315730 : bool ret = false;
7510 11315730 : Numeric_constant nc1, nc2;
7511 11315730 : if (l1->numeric_constant_value(&nc1)
7512 22631460 : && l2->numeric_constant_value(&nc2))
7513 : {
7514 11315730 : mpz_t v1;
7515 11315730 : if (nc1.to_int(&v1))
7516 : {
7517 11315730 : mpz_t v2;
7518 11315730 : if (nc2.to_int(&v2))
7519 : {
7520 11315730 : ret = mpz_cmp(v1, v2) == 0;
7521 11315730 : mpz_clear(v2);
7522 : }
7523 11315730 : mpz_clear(v1);
7524 : }
7525 : }
7526 11315730 : return ret;
7527 11315730 : }
7528 :
7529 : // Otherwise the arrays are not identical.
7530 : return false;
7531 : }
7532 :
7533 : // Message name.
7534 :
7535 : void
7536 11775 : Array_type::do_message_name(std::string* ret) const
7537 : {
7538 11775 : ret->push_back('[');
7539 11775 : if (!this->is_slice_type())
7540 : {
7541 686 : Numeric_constant nc;
7542 686 : if (!this->length_->numeric_constant_value(&nc))
7543 0 : ret->append("<unknown length>");
7544 : else
7545 : {
7546 686 : mpz_t val;
7547 686 : if (!nc.to_int(&val))
7548 0 : ret->append("<unknown length>");
7549 : else
7550 : {
7551 686 : char* s = mpz_get_str(NULL, 10, val);
7552 686 : ret->append(s);
7553 686 : free(s);
7554 686 : mpz_clear(val);
7555 : }
7556 : }
7557 686 : }
7558 11775 : ret->push_back(']');
7559 11775 : this->append_message_name(this->element_type_, ret);
7560 11775 : }
7561 :
7562 : // Traversal.
7563 :
7564 : int
7565 42947836 : Array_type::do_traverse(Traverse* traverse)
7566 : {
7567 42947836 : if (Type::traverse(this->element_type_, traverse) == TRAVERSE_EXIT)
7568 : return TRAVERSE_EXIT;
7569 42947719 : if (this->length_ != NULL
7570 42947719 : && Expression::traverse(&this->length_, traverse) == TRAVERSE_EXIT)
7571 : return TRAVERSE_EXIT;
7572 : return TRAVERSE_CONTINUE;
7573 : }
7574 :
7575 : // Check that the length is valid.
7576 :
7577 : bool
7578 508069 : Array_type::verify_length(Gogo* gogo)
7579 : {
7580 508069 : if (this->length_ == NULL)
7581 : return true;
7582 :
7583 232201 : Type* int_type = Type::lookup_integer_type("int");
7584 232201 : Type_context int_context(int_type, false);
7585 232201 : this->length_->determine_type(gogo, &int_context);
7586 :
7587 232201 : if (this->length_->is_error_expression()
7588 232201 : || this->length_->type()->is_error())
7589 : {
7590 4 : go_assert(saw_errors());
7591 : return false;
7592 : }
7593 :
7594 232197 : if (!this->length_->is_constant())
7595 : {
7596 2 : go_error_at(this->length_->location(), "array bound is not constant");
7597 2 : return false;
7598 : }
7599 :
7600 : // For array types, the length expression can be an untyped constant
7601 : // representable as an int, but we don't allow explicitly non-integer
7602 : // values such as "float64(10)". See issues #13485 and #13486.
7603 232195 : if (this->length_->type()->integer_type() == NULL
7604 4 : && !this->length_->type()->is_error_type())
7605 : {
7606 4 : go_error_at(this->length_->location(), "invalid array bound");
7607 4 : return false;
7608 : }
7609 :
7610 232191 : Numeric_constant nc;
7611 232191 : if (!this->length_->numeric_constant_value(&nc))
7612 : {
7613 0 : if (this->length_->type()->integer_type() != NULL
7614 0 : || this->length_->type()->float_type() != NULL)
7615 0 : go_error_at(this->length_->location(), "array bound is not constant");
7616 : else
7617 0 : go_error_at(this->length_->location(), "array bound is not numeric");
7618 0 : return false;
7619 : }
7620 :
7621 464382 : unsigned int tbits = int_type->integer_type()->bits();
7622 232191 : unsigned long val;
7623 232191 : switch (nc.to_unsigned_long(&val))
7624 : {
7625 232187 : case Numeric_constant::NC_UL_VALID:
7626 232187 : if (sizeof(val) >= tbits / 8 && val >> (tbits - 1) != 0)
7627 : {
7628 1 : go_error_at(this->length_->location(), "array bound overflows");
7629 1 : return false;
7630 : }
7631 : break;
7632 1 : case Numeric_constant::NC_UL_NOTINT:
7633 1 : go_error_at(this->length_->location(), "array bound truncated to integer");
7634 1 : return false;
7635 2 : case Numeric_constant::NC_UL_NEGATIVE:
7636 2 : go_error_at(this->length_->location(), "negative array bound");
7637 2 : return false;
7638 1 : case Numeric_constant::NC_UL_BIG:
7639 1 : {
7640 1 : mpz_t mval;
7641 1 : if (!nc.to_int(&mval))
7642 0 : go_unreachable();
7643 1 : unsigned int bits = mpz_sizeinbase(mval, 2);
7644 1 : mpz_clear(mval);
7645 1 : if (bits >= tbits)
7646 : {
7647 1 : go_error_at(this->length_->location(), "array bound overflows");
7648 1 : return false;
7649 : }
7650 : }
7651 0 : break;
7652 0 : default:
7653 0 : go_unreachable();
7654 : }
7655 :
7656 : return true;
7657 232191 : }
7658 :
7659 : // Verify the type.
7660 :
7661 : bool
7662 508071 : Array_type::do_verify(Gogo* gogo)
7663 : {
7664 508071 : if (this->element_type()->is_error_type())
7665 : {
7666 2 : this->set_is_error();
7667 2 : return false;
7668 : }
7669 508069 : if (!this->verify_length(gogo))
7670 : {
7671 15 : this->length_ = Expression::make_error(this->length_->location());
7672 15 : this->set_is_error();
7673 : }
7674 : return true;
7675 : }
7676 :
7677 : // Whether the type contains pointers. This is always true for a
7678 : // slice. For an array it is true if the element type has pointers
7679 : // and the length is greater than zero.
7680 :
7681 : bool
7682 1279733 : Array_type::do_has_pointer() const
7683 : {
7684 1279733 : if (this->length_ == NULL)
7685 : return true;
7686 381013 : if (!this->element_type_->has_pointer())
7687 : return false;
7688 :
7689 214855 : Numeric_constant nc;
7690 214855 : if (!this->length_->numeric_constant_value(&nc))
7691 : {
7692 : // Error reported elsewhere.
7693 : return false;
7694 : }
7695 :
7696 214855 : unsigned long val;
7697 214855 : switch (nc.to_unsigned_long(&val))
7698 : {
7699 214855 : case Numeric_constant::NC_UL_VALID:
7700 214855 : return val > 0;
7701 : case Numeric_constant::NC_UL_BIG:
7702 : return true;
7703 : default:
7704 : // Error reported elsewhere.
7705 : return false;
7706 : }
7707 214855 : }
7708 :
7709 : // Whether we can use memcmp to compare this array.
7710 :
7711 : bool
7712 499633 : Array_type::do_compare_is_identity(Gogo* gogo)
7713 : {
7714 499633 : if (this->length_ == NULL)
7715 : return false;
7716 :
7717 : // Check for [...], which indicates that this is not a real type.
7718 478882 : if (this->length_->is_nil_expression())
7719 : return false;
7720 :
7721 478882 : if (!this->element_type_->compare_is_identity(gogo))
7722 : return false;
7723 :
7724 : // If there is any padding, then we can't use memcmp.
7725 432783 : int64_t size;
7726 432783 : int64_t align;
7727 432783 : if (!this->element_type_->backend_type_size(gogo, &size)
7728 432783 : || !this->element_type_->backend_type_align(gogo, &align))
7729 0 : return false;
7730 432783 : if ((size & (align - 1)) != 0)
7731 : return false;
7732 :
7733 : return true;
7734 : }
7735 :
7736 : // Array type hash code.
7737 :
7738 : unsigned int
7739 2898543 : Array_type::do_hash_for_method(Gogo* gogo, int flags) const
7740 : {
7741 2898543 : unsigned int ret;
7742 :
7743 : // There is no very convenient way to get a hash code for the
7744 : // length.
7745 2898543 : ret = this->element_type_->hash_for_method(gogo, flags) + 1;
7746 2898543 : if (this->is_array_incomparable_)
7747 821459 : ret <<= 1;
7748 2898543 : return ret;
7749 : }
7750 :
7751 : // Write the hash function for an array which can not use the identify
7752 : // function.
7753 :
7754 : void
7755 27 : Array_type::write_hash_function(Gogo* gogo, Named_object* function,
7756 : Function_type* hash_fntype)
7757 : {
7758 27 : Location bloc = Linemap::predeclared_location();
7759 :
7760 : // The pointer to the array that we are going to hash. This is an
7761 : // argument to the hash function we are implementing here.
7762 27 : Named_object* key_arg = gogo->lookup("key", NULL);
7763 27 : go_assert(key_arg != NULL);
7764 27 : Type* key_arg_type = key_arg->var_value()->type();
7765 :
7766 : // The seed argument to the hash function.
7767 27 : Named_object* seed_arg = gogo->lookup("seed", NULL);
7768 27 : go_assert(seed_arg != NULL);
7769 :
7770 27 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
7771 :
7772 : // Make a temporary to hold the return value, initialized to the seed.
7773 27 : Expression* ref = Expression::make_var_reference(seed_arg, bloc);
7774 27 : Temporary_statement* retval = Statement::make_temporary(uintptr_type, ref,
7775 : bloc);
7776 27 : retval->determine_types(gogo);
7777 27 : gogo->add_statement(retval);
7778 :
7779 : // Make a temporary to hold the key as a uintptr.
7780 27 : ref = Expression::make_var_reference(key_arg, bloc);
7781 27 : ref = Expression::make_cast(uintptr_type, ref, bloc);
7782 27 : Temporary_statement* key = Statement::make_temporary(uintptr_type, ref,
7783 : bloc);
7784 27 : key->determine_types(gogo);
7785 27 : gogo->add_statement(key);
7786 :
7787 : // Loop over the array elements.
7788 : // for i = range a
7789 27 : Type* int_type = Type::lookup_integer_type("int");
7790 27 : Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7791 27 : index->determine_types(gogo);
7792 27 : gogo->add_statement(index);
7793 :
7794 27 : Expression* iref = Expression::make_temporary_reference(index, bloc);
7795 27 : Expression* aref = Expression::make_var_reference(key_arg, bloc);
7796 27 : Type* pt = Type::make_pointer_type(static_cast<Type*>(this));
7797 27 : aref = Expression::make_cast(pt, aref, bloc);
7798 27 : For_range_statement* for_range = Statement::make_for_range_statement(iref,
7799 : NULL,
7800 : aref,
7801 : bloc);
7802 :
7803 27 : gogo->start_block(bloc);
7804 :
7805 : // Get the hash function for the element type.
7806 27 : Named_object* hash_fn =
7807 27 : this->element_type_->unalias()->hash_function(gogo, hash_fntype);
7808 :
7809 : // Get a pointer to this element in the loop.
7810 27 : Expression* subkey = Expression::make_temporary_reference(key, bloc);
7811 27 : subkey = Expression::make_cast(key_arg_type, subkey, bloc);
7812 :
7813 : // Get the size of each element.
7814 27 : Expression* ele_size = Expression::make_type_info(this->element_type_,
7815 : Expression::TYPE_INFO_SIZE);
7816 :
7817 : // Get the hash of this element, passing retval as the seed.
7818 27 : ref = Expression::make_temporary_reference(retval, bloc);
7819 27 : Expression_list* args = new Expression_list();
7820 27 : args->push_back(subkey);
7821 27 : args->push_back(ref);
7822 27 : Expression* func = Expression::make_func_reference(hash_fn, NULL, bloc);
7823 27 : Expression* call = Expression::make_call(func, args, false, bloc);
7824 :
7825 : // Set retval to the result.
7826 27 : Temporary_reference_expression* tref =
7827 27 : Expression::make_temporary_reference(retval, bloc);
7828 27 : tref->set_is_lvalue();
7829 27 : Statement* s = Statement::make_assignment(tref, call, bloc);
7830 27 : s->determine_types(gogo);
7831 27 : gogo->add_statement(s);
7832 :
7833 : // Increase the element pointer.
7834 27 : tref = Expression::make_temporary_reference(key, bloc);
7835 27 : tref->set_is_lvalue();
7836 27 : s = Statement::make_assignment_operation(OPERATOR_PLUSEQ, tref, ele_size,
7837 : bloc);
7838 27 : Block* statements = gogo->finish_block(bloc);
7839 :
7840 27 : for_range->add_statements(statements);
7841 27 : for_range->determine_types(gogo);
7842 27 : gogo->add_statement(for_range);
7843 :
7844 : // Return retval to the caller of the hash function.
7845 27 : Expression_list* vals = new Expression_list();
7846 27 : ref = Expression::make_temporary_reference(retval, bloc);
7847 27 : vals->push_back(ref);
7848 27 : s = Statement::make_return_statement(function, vals, bloc);
7849 27 : s->determine_types(gogo);
7850 27 : gogo->add_statement(s);
7851 27 : }
7852 :
7853 : // Write the equality function for an array which can not use the
7854 : // identity function.
7855 :
7856 : void
7857 8659 : Array_type::write_equal_function(Gogo* gogo, Named_object* function,
7858 : Named_type* name)
7859 : {
7860 8659 : Location bloc = Linemap::predeclared_location();
7861 :
7862 : // The pointers to the arrays we are going to compare.
7863 8659 : Named_object* key1_arg = gogo->lookup("key1", NULL);
7864 8659 : Named_object* key2_arg = gogo->lookup("key2", NULL);
7865 8659 : go_assert(key1_arg != NULL && key2_arg != NULL);
7866 :
7867 : // Build temporaries for the keys with the right types.
7868 8659 : Type* pt = Type::make_pointer_type(name != NULL
7869 : ? static_cast<Type*>(name)
7870 : : static_cast<Type*>(this));
7871 :
7872 8659 : Expression* ref = Expression::make_var_reference(key1_arg, bloc);
7873 8659 : ref = Expression::make_unsafe_cast(pt, ref, bloc);
7874 8659 : Temporary_statement* p1 = Statement::make_temporary(pt, ref, bloc);
7875 8659 : p1->determine_types(gogo);
7876 8659 : gogo->add_statement(p1);
7877 :
7878 8659 : ref = Expression::make_var_reference(key2_arg, bloc);
7879 8659 : ref = Expression::make_unsafe_cast(pt, ref, bloc);
7880 8659 : Temporary_statement* p2 = Statement::make_temporary(pt, ref, bloc);
7881 8659 : p2->determine_types(gogo);
7882 8659 : gogo->add_statement(p2);
7883 :
7884 : // Loop over the array elements.
7885 : // for i = range a
7886 8659 : Type* int_type = Type::lookup_integer_type("int");
7887 8659 : Temporary_statement* index = Statement::make_temporary(int_type, NULL, bloc);
7888 8659 : index->determine_types(gogo);
7889 8659 : gogo->add_statement(index);
7890 :
7891 8659 : Expression* iref = Expression::make_temporary_reference(index, bloc);
7892 8659 : Expression* aref = Expression::make_temporary_reference(p1, bloc);
7893 8659 : For_range_statement* for_range = Statement::make_for_range_statement(iref,
7894 : NULL,
7895 : aref,
7896 : bloc);
7897 :
7898 8659 : gogo->start_block(bloc);
7899 :
7900 : // Compare element in P1 and P2.
7901 8659 : Expression* e1 = Expression::make_temporary_reference(p1, bloc);
7902 8659 : e1 = Expression::make_dereference(e1, Expression::NIL_CHECK_DEFAULT, bloc);
7903 8659 : ref = Expression::make_temporary_reference(index, bloc);
7904 8659 : e1 = Expression::make_array_index(e1, ref, NULL, NULL, bloc);
7905 :
7906 8659 : Expression* e2 = Expression::make_temporary_reference(p2, bloc);
7907 8659 : e2 = Expression::make_dereference(e2, Expression::NIL_CHECK_DEFAULT, bloc);
7908 8659 : ref = Expression::make_temporary_reference(index, bloc);
7909 8659 : e2 = Expression::make_array_index(e2, ref, NULL, NULL, bloc);
7910 :
7911 8659 : Expression* cond = Expression::make_binary(OPERATOR_NOTEQ, e1, e2, bloc);
7912 :
7913 : // If the elements are not equal, return false.
7914 8659 : gogo->start_block(bloc);
7915 8659 : Expression_list* vals = new Expression_list();
7916 8659 : vals->push_back(Expression::make_boolean(false, bloc));
7917 8659 : Statement* s = Statement::make_return_statement(function, vals, bloc);
7918 8659 : s->determine_types(gogo);
7919 8659 : gogo->add_statement(s);
7920 8659 : Block* then_block = gogo->finish_block(bloc);
7921 :
7922 8659 : s = Statement::make_if_statement(cond, then_block, NULL, bloc);
7923 8659 : s->determine_types(gogo);
7924 8659 : gogo->add_statement(s);
7925 :
7926 8659 : Block* statements = gogo->finish_block(bloc);
7927 :
7928 8659 : for_range->add_statements(statements);
7929 8659 : for_range->determine_types(gogo);
7930 8659 : gogo->add_statement(for_range);
7931 :
7932 : // All the elements are equal, so return true.
7933 8659 : vals = new Expression_list();
7934 8659 : vals->push_back(Expression::make_boolean(true, bloc));
7935 8659 : s = Statement::make_return_statement(function, vals, bloc);
7936 8659 : s->determine_types(gogo);
7937 8659 : gogo->add_statement(s);
7938 8659 : }
7939 :
7940 : // Get the backend representation of the fields of a slice. This is
7941 : // not declared in types.h so that types.h doesn't have to #include
7942 : // backend.h.
7943 : //
7944 : // We use int for the count and capacity fields. This matches 6g.
7945 : // The language more or less assumes that we can't allocate space of a
7946 : // size which does not fit in int.
7947 :
7948 : static void
7949 109591 : get_backend_slice_fields(Gogo* gogo, Array_type* type, bool use_placeholder,
7950 : std::vector<Backend::Btyped_identifier>* bfields)
7951 : {
7952 109591 : bfields->resize(3);
7953 :
7954 109591 : Type* pet = Type::make_pointer_type(type->element_type());
7955 109591 : Btype* pbet = (use_placeholder
7956 109591 : ? pet->get_backend_placeholder(gogo)
7957 21653 : : pet->get_backend(gogo));
7958 109591 : Location ploc = Linemap::predeclared_location();
7959 :
7960 109591 : Backend::Btyped_identifier* p = &(*bfields)[0];
7961 109591 : p->name = "__values";
7962 109591 : p->btype = pbet;
7963 109591 : p->location = ploc;
7964 :
7965 109591 : Type* int_type = Type::lookup_integer_type("int");
7966 :
7967 109591 : p = &(*bfields)[1];
7968 109591 : p->name = "__count";
7969 109591 : p->btype = int_type->get_backend(gogo);
7970 109591 : p->location = ploc;
7971 :
7972 109591 : p = &(*bfields)[2];
7973 109591 : p->name = "__capacity";
7974 109591 : p->btype = int_type->get_backend(gogo);
7975 109591 : p->location = ploc;
7976 109591 : }
7977 :
7978 : // Get the backend representation for the type of this array. A fixed array is
7979 : // simply represented as ARRAY_TYPE with the appropriate index--i.e., it is
7980 : // just like an array in C. An open array is a struct with three
7981 : // fields: a data pointer, the length, and the capacity.
7982 :
7983 : Btype*
7984 142729 : Array_type::do_get_backend(Gogo* gogo)
7985 : {
7986 142729 : if (this->length_ == NULL)
7987 : {
7988 21653 : std::vector<Backend::Btyped_identifier> bfields;
7989 21653 : get_backend_slice_fields(gogo, this, false, &bfields);
7990 21653 : return gogo->backend()->struct_type(bfields);
7991 21653 : }
7992 : else
7993 : {
7994 121076 : Btype* element = this->get_backend_element(gogo, false);
7995 121076 : Bexpression* len = this->get_backend_length(gogo);
7996 121076 : return gogo->backend()->array_type(element, len);
7997 : }
7998 : }
7999 :
8000 : // Return the backend representation of the element type.
8001 :
8002 : Btype*
8003 215930 : Array_type::get_backend_element(Gogo* gogo, bool use_placeholder)
8004 : {
8005 215930 : if (use_placeholder)
8006 94854 : return this->element_type_->get_backend_placeholder(gogo);
8007 : else
8008 121076 : return this->element_type_->get_backend(gogo);
8009 : }
8010 :
8011 : // Return the backend representation of the length. The length may be
8012 : // computed using a function call, so we must only evaluate it once.
8013 :
8014 : Bexpression*
8015 215930 : Array_type::get_backend_length(Gogo* gogo)
8016 : {
8017 215930 : go_assert(this->length_ != NULL);
8018 215930 : if (this->blength_ == NULL)
8019 : {
8020 205230 : if (this->length_->is_error_expression())
8021 : {
8022 0 : this->blength_ = gogo->backend()->error_expression();
8023 0 : return this->blength_;
8024 : }
8025 205230 : Numeric_constant nc;
8026 205230 : mpz_t val;
8027 205230 : if (this->length_->numeric_constant_value(&nc) && nc.to_int(&val))
8028 : {
8029 205230 : if (mpz_sgn(val) < 0)
8030 : {
8031 0 : this->blength_ = gogo->backend()->error_expression();
8032 0 : return this->blength_;
8033 : }
8034 205230 : Type* t = nc.type();
8035 205230 : if (t == NULL)
8036 0 : t = Type::lookup_integer_type("int");
8037 205230 : else if (t->is_abstract())
8038 28365 : t = t->make_non_abstract_type();
8039 205230 : Btype* btype = t->get_backend(gogo);
8040 205230 : this->blength_ =
8041 205230 : gogo->backend()->integer_constant_expression(btype, val);
8042 205230 : mpz_clear(val);
8043 : }
8044 : else
8045 : {
8046 : // Make up a translation context for the array length
8047 : // expression. FIXME: This won't work in general.
8048 0 : Translate_context context(gogo, NULL, NULL, NULL);
8049 0 : this->blength_ = this->length_->get_backend(&context);
8050 :
8051 0 : Btype* ibtype = Type::lookup_integer_type("int")->get_backend(gogo);
8052 0 : this->blength_ =
8053 0 : gogo->backend()->convert_expression(ibtype, this->blength_,
8054 0 : this->length_->location());
8055 : }
8056 205230 : }
8057 215930 : return this->blength_;
8058 : }
8059 :
8060 : // Finish backend representation of the array.
8061 :
8062 : void
8063 121396 : Array_type::finish_backend_element(Gogo* gogo)
8064 : {
8065 242792 : Type* et = this->array_type()->element_type();
8066 121396 : et->get_backend(gogo);
8067 121396 : if (this->is_slice_type())
8068 : {
8069 : // This relies on the fact that we always use the same
8070 : // structure for a pointer to any given type.
8071 54806 : Type* pet = Type::make_pointer_type(et);
8072 54806 : pet->get_backend(gogo);
8073 : }
8074 121396 : }
8075 :
8076 : // Return an expression for a pointer to the values in ARRAY.
8077 :
8078 : Expression*
8079 159384 : Array_type::get_value_pointer(Gogo*, Expression* array) const
8080 : {
8081 159384 : if (this->length() != NULL)
8082 : {
8083 : // Fixed array.
8084 12321 : go_assert(array->type()->array_type() != NULL);
8085 24642 : Type* etype = array->type()->array_type()->element_type();
8086 12321 : array = Expression::make_unary(OPERATOR_AND, array, array->location());
8087 12321 : return Expression::make_cast(Type::make_pointer_type(etype), array,
8088 12321 : array->location());
8089 : }
8090 :
8091 : // Slice.
8092 147063 : return Expression::make_slice_info(array,
8093 : Expression::SLICE_INFO_VALUE_POINTER,
8094 147063 : array->location());
8095 : }
8096 :
8097 : // Return an expression for the length of the array ARRAY which has this
8098 : // type.
8099 :
8100 : Expression*
8101 431785 : Array_type::get_length(Gogo*, Expression* array) const
8102 : {
8103 431785 : if (this->length_ != NULL)
8104 : return this->length_;
8105 :
8106 : // This is a slice. We need to read the length field.
8107 277966 : return Expression::make_slice_info(array, Expression::SLICE_INFO_LENGTH,
8108 277966 : array->location());
8109 : }
8110 :
8111 : // Return an expression for the capacity of the array ARRAY which has this
8112 : // type.
8113 :
8114 : Expression*
8115 162952 : Array_type::get_capacity(Gogo*, Expression* array) const
8116 : {
8117 162952 : if (this->length_ != NULL)
8118 : return this->length_;
8119 :
8120 : // This is a slice. We need to read the capacity field.
8121 150628 : return Expression::make_slice_info(array, Expression::SLICE_INFO_CAPACITY,
8122 150628 : array->location());
8123 : }
8124 :
8125 : // Export.
8126 :
8127 : void
8128 28104 : Array_type::do_export(Export* exp) const
8129 : {
8130 28104 : exp->write_c_string("[");
8131 28104 : if (this->length_ != NULL)
8132 : {
8133 7110 : Numeric_constant nc;
8134 7110 : mpz_t val;
8135 7110 : if (!this->length_->numeric_constant_value(&nc) || !nc.to_int(&val))
8136 : {
8137 0 : go_assert(saw_errors());
8138 0 : return;
8139 : }
8140 7110 : char* s = mpz_get_str(NULL, 10, val);
8141 7110 : exp->write_string(s);
8142 7110 : free(s);
8143 7110 : exp->write_string(" ");
8144 7110 : mpz_clear(val);
8145 7110 : }
8146 28104 : exp->write_c_string("] ");
8147 28104 : exp->write_type(this->element_type_);
8148 : }
8149 :
8150 : // Import.
8151 :
8152 : Array_type*
8153 269509 : Array_type::do_import(Import* imp)
8154 : {
8155 269509 : imp->require_c_string("[");
8156 269509 : Expression* length;
8157 269509 : if (imp->peek_char() == ']')
8158 : length = NULL;
8159 : else
8160 134105 : length = Expression::import_expression(imp, imp->location());
8161 269509 : imp->require_c_string("] ");
8162 269509 : Type* element_type = imp->read_type();
8163 269509 : return Type::make_array_type(element_type, length);
8164 : }
8165 :
8166 : // The type of an array type descriptor.
8167 :
8168 : Type*
8169 24702 : Array_type::make_array_type_descriptor_type()
8170 : {
8171 24702 : static Type* ret;
8172 24702 : if (ret == NULL)
8173 : {
8174 4646 : Type* tdt = Type::make_type_descriptor_type();
8175 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
8176 :
8177 4646 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
8178 :
8179 4646 : Struct_type* sf =
8180 4646 : Type::make_builtin_struct_type(4,
8181 : "", tdt,
8182 : "elem", ptdt,
8183 : "slice", ptdt,
8184 : "len", uintptr_type);
8185 :
8186 4646 : ret = Type::make_builtin_named_type("ArrayType", sf);
8187 : }
8188 :
8189 24702 : return ret;
8190 : }
8191 :
8192 : // The type of an slice type descriptor.
8193 :
8194 : Type*
8195 19359 : Array_type::make_slice_type_descriptor_type()
8196 : {
8197 19359 : static Type* ret;
8198 19359 : if (ret == NULL)
8199 : {
8200 4646 : Type* tdt = Type::make_type_descriptor_type();
8201 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
8202 :
8203 4646 : Struct_type* sf =
8204 4646 : Type::make_builtin_struct_type(2,
8205 : "", tdt,
8206 : "elem", ptdt);
8207 :
8208 4646 : ret = Type::make_builtin_named_type("SliceType", sf);
8209 : }
8210 :
8211 19359 : return ret;
8212 : }
8213 :
8214 : // Build a type descriptor for an array/slice type.
8215 :
8216 : Expression*
8217 34769 : Array_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8218 : {
8219 34769 : if (this->length_ != NULL)
8220 20056 : return this->array_type_descriptor(gogo, name);
8221 : else
8222 14713 : return this->slice_type_descriptor(gogo, name);
8223 : }
8224 :
8225 : // Build a type descriptor for an array type.
8226 :
8227 : Expression*
8228 20056 : Array_type::array_type_descriptor(Gogo* gogo, Named_type* name)
8229 : {
8230 20056 : Location bloc = Linemap::predeclared_location();
8231 :
8232 20056 : Type* atdt = Array_type::make_array_type_descriptor_type();
8233 :
8234 40112 : const Struct_field_list* fields = atdt->struct_type()->fields();
8235 :
8236 20056 : Expression_list* vals = new Expression_list();
8237 20056 : vals->reserve(3);
8238 :
8239 20056 : Struct_field_list::const_iterator p = fields->begin();
8240 20056 : go_assert(p->is_field_name("_type"));
8241 20056 : vals->push_back(this->type_descriptor_constructor(gogo,
8242 : RUNTIME_TYPE_KIND_ARRAY,
8243 : name, NULL, true));
8244 :
8245 20056 : ++p;
8246 20056 : go_assert(p->is_field_name("elem"));
8247 20056 : vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
8248 :
8249 20056 : ++p;
8250 20056 : go_assert(p->is_field_name("slice"));
8251 20056 : Type* slice_type = Type::make_array_type(this->element_type_, NULL);
8252 20056 : vals->push_back(Expression::make_type_descriptor(slice_type, bloc));
8253 :
8254 20056 : ++p;
8255 20056 : go_assert(p->is_field_name("len"));
8256 20056 : vals->push_back(Expression::make_cast(p->type(), this->length_, bloc));
8257 :
8258 20056 : ++p;
8259 20056 : go_assert(p == fields->end());
8260 :
8261 20056 : return Expression::make_struct_composite_literal(atdt, vals, bloc);
8262 : }
8263 :
8264 : // Build a type descriptor for a slice type.
8265 :
8266 : Expression*
8267 14713 : Array_type::slice_type_descriptor(Gogo* gogo, Named_type* name)
8268 : {
8269 14713 : Location bloc = Linemap::predeclared_location();
8270 :
8271 14713 : Type* stdt = Array_type::make_slice_type_descriptor_type();
8272 :
8273 29426 : const Struct_field_list* fields = stdt->struct_type()->fields();
8274 :
8275 14713 : Expression_list* vals = new Expression_list();
8276 14713 : vals->reserve(2);
8277 :
8278 14713 : Struct_field_list::const_iterator p = fields->begin();
8279 14713 : go_assert(p->is_field_name("_type"));
8280 14713 : vals->push_back(this->type_descriptor_constructor(gogo,
8281 : RUNTIME_TYPE_KIND_SLICE,
8282 : name, NULL, true));
8283 :
8284 14713 : ++p;
8285 14713 : go_assert(p->is_field_name("elem"));
8286 14713 : vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
8287 :
8288 14713 : ++p;
8289 14713 : go_assert(p == fields->end());
8290 :
8291 14713 : return Expression::make_struct_composite_literal(stdt, vals, bloc);
8292 : }
8293 :
8294 : // Reflection string.
8295 :
8296 : void
8297 147009 : Array_type::do_reflection(Gogo* gogo, std::string* ret) const
8298 : {
8299 147009 : ret->push_back('[');
8300 147009 : if (this->length_ != NULL)
8301 : {
8302 32414 : Numeric_constant nc;
8303 32414 : if (!this->length_->numeric_constant_value(&nc))
8304 : {
8305 0 : go_assert(saw_errors());
8306 : return;
8307 : }
8308 32414 : mpz_t val;
8309 32414 : if (!nc.to_int(&val))
8310 : {
8311 0 : go_assert(saw_errors());
8312 : return;
8313 : }
8314 32414 : char* s = mpz_get_str(NULL, 10, val);
8315 32414 : ret->append(s);
8316 32414 : free(s);
8317 32414 : mpz_clear(val);
8318 32414 : }
8319 147009 : ret->push_back(']');
8320 :
8321 147009 : this->append_reflection(this->element_type_, gogo, ret);
8322 : }
8323 :
8324 : // Make an array type.
8325 :
8326 : Array_type*
8327 1014774 : Type::make_array_type(Type* element_type, Expression* length)
8328 : {
8329 1014774 : return new Array_type(element_type, length);
8330 : }
8331 :
8332 : // Class Map_type.
8333 :
8334 : Named_object* Map_type::zero_value;
8335 : int64_t Map_type::zero_value_size;
8336 : int64_t Map_type::zero_value_align;
8337 :
8338 : // If this map requires the "fat" functions, return the pointer to
8339 : // pass as the zero value to those functions. Otherwise, in the
8340 : // normal case, return NULL. The map requires the "fat" functions if
8341 : // the value size is larger than max_zero_size bytes. max_zero_size
8342 : // must match maxZero in libgo/go/runtime/map.go.
8343 :
8344 : Expression*
8345 8227 : Map_type::fat_zero_value(Gogo* gogo)
8346 : {
8347 8227 : int64_t valsize;
8348 8227 : if (!this->val_type_->backend_type_size(gogo, &valsize))
8349 : {
8350 0 : go_assert(saw_errors());
8351 : return NULL;
8352 : }
8353 8227 : if (valsize <= Map_type::max_zero_size)
8354 : return NULL;
8355 :
8356 6 : if (Map_type::zero_value_size < valsize)
8357 3 : Map_type::zero_value_size = valsize;
8358 :
8359 6 : int64_t valalign;
8360 6 : if (!this->val_type_->backend_type_align(gogo, &valalign))
8361 : {
8362 0 : go_assert(saw_errors());
8363 : return NULL;
8364 : }
8365 :
8366 6 : if (Map_type::zero_value_align < valalign)
8367 3 : Map_type::zero_value_align = valalign;
8368 :
8369 6 : Location bloc = Linemap::predeclared_location();
8370 :
8371 6 : if (Map_type::zero_value == NULL)
8372 : {
8373 : // The final type will be set in backend_zero_value.
8374 3 : Type* uint8_type = Type::lookup_integer_type("uint8");
8375 3 : Expression* size = Expression::make_integer_ul(0, NULL, bloc);
8376 3 : Array_type* array_type = Type::make_array_type(uint8_type, size);
8377 3 : array_type->set_is_array_incomparable();
8378 3 : Variable* var = new Variable(array_type, NULL, true, false, false, bloc);
8379 3 : std::string name = gogo->map_zero_value_name();
8380 3 : Map_type::zero_value = Named_object::make_variable(name, NULL, var);
8381 3 : }
8382 :
8383 6 : Expression* z = Expression::make_var_reference(Map_type::zero_value, bloc);
8384 6 : z = Expression::make_unary(OPERATOR_AND, z, bloc);
8385 6 : Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
8386 6 : z = Expression::make_cast(unsafe_ptr_type, z, bloc);
8387 6 : return z;
8388 : }
8389 :
8390 : // Map algorithm to use for this map type.
8391 :
8392 : Map_type::Map_alg
8393 14744 : Map_type::algorithm(Gogo* gogo)
8394 : {
8395 14744 : int64_t size;
8396 14744 : bool ok = this->val_type_->backend_type_size(gogo, &size);
8397 14744 : if (!ok || size > Map_type::max_val_size)
8398 : return MAP_ALG_SLOW;
8399 :
8400 14686 : Type* key_type = this->key_type_;
8401 14686 : if (key_type->is_string_type())
8402 : return MAP_ALG_FASTSTR;
8403 7515 : if (!key_type->compare_is_identity(gogo))
8404 : return MAP_ALG_SLOW;
8405 :
8406 4713 : ok = key_type->backend_type_size(gogo, &size);
8407 4713 : if (!ok)
8408 : return MAP_ALG_SLOW;
8409 4713 : if (size == 4)
8410 1602 : return (key_type->has_pointer()
8411 1602 : ? MAP_ALG_FAST32PTR
8412 : : MAP_ALG_FAST32);
8413 3111 : if (size == 8)
8414 : {
8415 2785 : if (!key_type->has_pointer())
8416 : return MAP_ALG_FAST64;
8417 849 : Type* ptr_type = Type::make_pointer_type(Type::make_void_type());
8418 849 : ok = ptr_type->backend_type_size(gogo, &size);
8419 849 : if (ok && size == 8)
8420 : return MAP_ALG_FAST64PTR;
8421 : // Key contains pointer but is not a single pointer.
8422 : // Use slow version.
8423 : }
8424 : return MAP_ALG_SLOW;
8425 : }
8426 :
8427 : // Return whether VAR is the map zero value.
8428 :
8429 : bool
8430 923411 : Map_type::is_zero_value(Variable* var)
8431 : {
8432 923411 : return (Map_type::zero_value != NULL
8433 923411 : && Map_type::zero_value->var_value() == var);
8434 : }
8435 :
8436 : // Return the backend representation for the zero value.
8437 :
8438 : Bvariable*
8439 3 : Map_type::backend_zero_value(Gogo* gogo)
8440 : {
8441 3 : Location bloc = Linemap::predeclared_location();
8442 :
8443 3 : go_assert(Map_type::zero_value != NULL);
8444 :
8445 3 : Type* uint8_type = Type::lookup_integer_type("uint8");
8446 3 : Btype* buint8_type = uint8_type->get_backend(gogo);
8447 :
8448 3 : Type* int_type = Type::lookup_integer_type("int");
8449 :
8450 3 : Expression* e = Expression::make_integer_int64(Map_type::zero_value_size,
8451 : int_type, bloc);
8452 3 : Translate_context context(gogo, NULL, NULL, NULL);
8453 3 : Bexpression* blength = e->get_backend(&context);
8454 :
8455 3 : Btype* barray_type = gogo->backend()->array_type(buint8_type, blength);
8456 :
8457 3 : std::string zname = Map_type::zero_value->name();
8458 3 : unsigned int flags = Backend::variable_is_common;
8459 3 : Bvariable* zvar =
8460 3 : gogo->backend()->implicit_variable(zname, "", barray_type, flags,
8461 : Map_type::zero_value_align);
8462 3 : gogo->backend()->implicit_variable_set_init(zvar, zname, barray_type,
8463 : flags, NULL);
8464 3 : return zvar;
8465 3 : }
8466 :
8467 : // Message name.
8468 :
8469 : void
8470 26 : Map_type::do_message_name(std::string* ret) const
8471 : {
8472 26 : ret->append("map[");
8473 26 : this->append_message_name(this->key_type_, ret);
8474 26 : ret->push_back(']');
8475 26 : this->append_message_name(this->val_type_, ret);
8476 26 : }
8477 :
8478 : // Traversal.
8479 :
8480 : int
8481 13330624 : Map_type::do_traverse(Traverse* traverse)
8482 : {
8483 13330624 : if (Type::traverse(this->key_type_, traverse) == TRAVERSE_EXIT
8484 13330624 : || Type::traverse(this->val_type_, traverse) == TRAVERSE_EXIT)
8485 200 : return TRAVERSE_EXIT;
8486 : return TRAVERSE_CONTINUE;
8487 : }
8488 :
8489 : // Check that the map type is OK.
8490 :
8491 : bool
8492 46285 : Map_type::do_verify(Gogo*)
8493 : {
8494 : // The runtime support uses "map[void]void".
8495 46285 : if (!this->key_type_->is_comparable() && !this->key_type_->is_void_type())
8496 : {
8497 13 : go_error_at(this->location_, "invalid map key type");
8498 13 : this->set_is_error();
8499 : }
8500 46285 : if (!this->key_type_->in_heap())
8501 : {
8502 0 : go_error_at(this->location_, "go:notinheap map key not allowed");
8503 0 : this->set_is_error();
8504 : }
8505 46285 : if (!this->val_type_->in_heap())
8506 : {
8507 0 : go_error_at(this->location_, "go:notinheap map value not allowed");
8508 0 : this->set_is_error();
8509 : }
8510 46285 : return true;
8511 : }
8512 :
8513 : // Whether two map types are identical.
8514 :
8515 : bool
8516 42492 : Map_type::is_identical(const Map_type* t, int flags) const
8517 : {
8518 42492 : return (Type::are_identical(this->key_type(), t->key_type(), flags, NULL)
8519 42492 : && Type::are_identical(this->val_type(), t->val_type(), flags,
8520 42492 : NULL));
8521 : }
8522 :
8523 : // Hash code.
8524 :
8525 : unsigned int
8526 120090 : Map_type::do_hash_for_method(Gogo* gogo, int flags) const
8527 : {
8528 120090 : return (this->key_type_->hash_for_method(gogo, flags)
8529 120090 : + this->val_type_->hash_for_method(gogo, flags)
8530 120090 : + 2);
8531 : }
8532 :
8533 : // Get the backend representation for a map type. A map type is
8534 : // represented as a pointer to a struct. The struct is hmap in
8535 : // runtime/map.go.
8536 :
8537 : Btype*
8538 21568 : Map_type::do_get_backend(Gogo* gogo)
8539 : {
8540 21568 : static Btype* backend_map_type;
8541 21568 : if (backend_map_type == NULL)
8542 : {
8543 2001 : std::vector<Backend::Btyped_identifier> bfields(9);
8544 :
8545 2001 : Location bloc = Linemap::predeclared_location();
8546 :
8547 2001 : Type* int_type = Type::lookup_integer_type("int");
8548 2001 : bfields[0].name = "count";
8549 2001 : bfields[0].btype = int_type->get_backend(gogo);
8550 2001 : bfields[0].location = bloc;
8551 :
8552 2001 : Type* uint8_type = Type::lookup_integer_type("uint8");
8553 2001 : bfields[1].name = "flags";
8554 2001 : bfields[1].btype = uint8_type->get_backend(gogo);
8555 2001 : bfields[1].location = bloc;
8556 :
8557 2001 : bfields[2].name = "B";
8558 2001 : bfields[2].btype = bfields[1].btype;
8559 2001 : bfields[2].location = bloc;
8560 :
8561 2001 : Type* uint16_type = Type::lookup_integer_type("uint16");
8562 2001 : bfields[3].name = "noverflow";
8563 2001 : bfields[3].btype = uint16_type->get_backend(gogo);
8564 2001 : bfields[3].location = bloc;
8565 :
8566 2001 : Type* uint32_type = Type::lookup_integer_type("uint32");
8567 2001 : bfields[4].name = "hash0";
8568 2001 : bfields[4].btype = uint32_type->get_backend(gogo);
8569 2001 : bfields[4].location = bloc;
8570 :
8571 2001 : Btype* bvt = gogo->backend()->void_type();
8572 2001 : Btype* bpvt = gogo->backend()->pointer_type(bvt);
8573 2001 : bfields[5].name = "buckets";
8574 2001 : bfields[5].btype = bpvt;
8575 2001 : bfields[5].location = bloc;
8576 :
8577 2001 : bfields[6].name = "oldbuckets";
8578 2001 : bfields[6].btype = bpvt;
8579 2001 : bfields[6].location = bloc;
8580 :
8581 2001 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
8582 2001 : bfields[7].name = "nevacuate";
8583 2001 : bfields[7].btype = uintptr_type->get_backend(gogo);
8584 2001 : bfields[7].location = bloc;
8585 :
8586 2001 : bfields[8].name = "extra";
8587 2001 : bfields[8].btype = bpvt;
8588 2001 : bfields[8].location = bloc;
8589 :
8590 2001 : Btype *bt = gogo->backend()->struct_type(bfields);
8591 2001 : bt = gogo->backend()->named_type("runtime.hmap", bt, bloc);
8592 2001 : backend_map_type = gogo->backend()->pointer_type(bt);
8593 2001 : }
8594 21568 : return backend_map_type;
8595 : }
8596 :
8597 : // The type of a map type descriptor.
8598 :
8599 : Type*
8600 8716 : Map_type::make_map_type_descriptor_type()
8601 : {
8602 8716 : static Type* ret;
8603 8716 : if (ret == NULL)
8604 : {
8605 4646 : Type* tdt = Type::make_type_descriptor_type();
8606 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
8607 4646 : Type* uint8_type = Type::lookup_integer_type("uint8");
8608 4646 : Type* uint16_type = Type::lookup_integer_type("uint16");
8609 4646 : Type* uint32_type = Type::lookup_integer_type("uint32");
8610 4646 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
8611 4646 : Type* void_type = Type::make_void_type();
8612 4646 : Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
8613 :
8614 4646 : Location bloc = Linemap::predeclared_location();
8615 4646 : Typed_identifier_list *params = new Typed_identifier_list();
8616 9292 : params->push_back(Typed_identifier("key", unsafe_pointer_type, bloc));
8617 9292 : params->push_back(Typed_identifier("seed", uintptr_type, bloc));
8618 :
8619 4646 : Typed_identifier_list* results = new Typed_identifier_list();
8620 9292 : results->push_back(Typed_identifier("", uintptr_type, bloc));
8621 :
8622 4646 : Type* hasher_fntype = Type::make_function_type(NULL, params, results,
8623 : bloc);
8624 :
8625 4646 : Struct_type* sf =
8626 4646 : Type::make_builtin_struct_type(9,
8627 : "", tdt,
8628 : "key", ptdt,
8629 : "elem", ptdt,
8630 : "bucket", ptdt,
8631 : "hasher", hasher_fntype,
8632 : "keysize", uint8_type,
8633 : "valuesize", uint8_type,
8634 : "bucketsize", uint16_type,
8635 : "flags", uint32_type);
8636 :
8637 4646 : ret = Type::make_builtin_named_type("MapType", sf);
8638 : }
8639 :
8640 8716 : return ret;
8641 : }
8642 :
8643 : // Build a type descriptor for a map type.
8644 :
8645 : Expression*
8646 4070 : Map_type::do_type_descriptor(Gogo* gogo, Named_type* name)
8647 : {
8648 4070 : Location bloc = Linemap::predeclared_location();
8649 :
8650 4070 : Type* mtdt = Map_type::make_map_type_descriptor_type();
8651 4070 : Type* uint8_type = Type::lookup_integer_type("uint8");
8652 4070 : Type* uint16_type = Type::lookup_integer_type("uint16");
8653 4070 : Type* uint32_type = Type::lookup_integer_type("uint32");
8654 :
8655 4070 : int64_t keysize;
8656 4070 : if (!this->key_type_->backend_type_size(gogo, &keysize))
8657 : {
8658 0 : go_error_at(this->location_, "error determining map key type size");
8659 0 : return Expression::make_error(this->location_);
8660 : }
8661 :
8662 4070 : int64_t valsize;
8663 4070 : if (!this->val_type_->backend_type_size(gogo, &valsize))
8664 : {
8665 0 : go_error_at(this->location_, "error determining map value type size");
8666 0 : return Expression::make_error(this->location_);
8667 : }
8668 :
8669 4070 : int64_t ptrsize;
8670 4070 : if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptrsize))
8671 : {
8672 0 : go_assert(saw_errors());
8673 0 : return Expression::make_error(this->location_);
8674 : }
8675 :
8676 4070 : Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8677 4070 : if (bucket_type == NULL)
8678 : {
8679 0 : go_assert(saw_errors());
8680 0 : return Expression::make_error(this->location_);
8681 : }
8682 :
8683 4070 : int64_t bucketsize;
8684 4070 : if (!bucket_type->backend_type_size(gogo, &bucketsize))
8685 : {
8686 0 : go_assert(saw_errors());
8687 0 : return Expression::make_error(this->location_);
8688 : }
8689 :
8690 8140 : const Struct_field_list* fields = mtdt->struct_type()->fields();
8691 :
8692 4070 : Expression_list* vals = new Expression_list();
8693 4070 : vals->reserve(12);
8694 :
8695 4070 : Struct_field_list::const_iterator p = fields->begin();
8696 4070 : go_assert(p->is_field_name("_type"));
8697 4070 : vals->push_back(this->type_descriptor_constructor(gogo,
8698 : RUNTIME_TYPE_KIND_MAP,
8699 : name, NULL, true));
8700 :
8701 4070 : ++p;
8702 4070 : go_assert(p->is_field_name("key"));
8703 4070 : vals->push_back(Expression::make_type_descriptor(this->key_type_, bloc));
8704 :
8705 4070 : ++p;
8706 4070 : go_assert(p->is_field_name("elem"));
8707 4070 : vals->push_back(Expression::make_type_descriptor(this->val_type_, bloc));
8708 :
8709 4070 : ++p;
8710 4070 : go_assert(p->is_field_name("bucket"));
8711 4070 : vals->push_back(Expression::make_type_descriptor(bucket_type, bloc));
8712 :
8713 4070 : ++p;
8714 4070 : go_assert(p->is_field_name("hasher"));
8715 4070 : Function_type* hasher_fntype = p->type()->function_type();
8716 4070 : Named_object* hasher_fn =
8717 4070 : this->key_type_->unalias()->hash_function(gogo, hasher_fntype);
8718 4070 : if (hasher_fn == NULL)
8719 0 : vals->push_back(Expression::make_cast(hasher_fntype,
8720 : Expression::make_nil(bloc),
8721 : bloc));
8722 : else
8723 4070 : vals->push_back(Expression::make_func_reference(hasher_fn, NULL, bloc));
8724 :
8725 4070 : ++p;
8726 4070 : go_assert(p->is_field_name("keysize"));
8727 4070 : if (keysize > Map_type::max_key_size)
8728 19 : vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8729 : else
8730 4051 : vals->push_back(Expression::make_integer_int64(keysize, uint8_type, bloc));
8731 :
8732 4070 : ++p;
8733 4070 : go_assert(p->is_field_name("valuesize"));
8734 4070 : if (valsize > Map_type::max_val_size)
8735 29 : vals->push_back(Expression::make_integer_int64(ptrsize, uint8_type, bloc));
8736 : else
8737 4041 : vals->push_back(Expression::make_integer_int64(valsize, uint8_type, bloc));
8738 :
8739 4070 : ++p;
8740 4070 : go_assert(p->is_field_name("bucketsize"));
8741 4070 : vals->push_back(Expression::make_integer_int64(bucketsize, uint16_type,
8742 : bloc));
8743 :
8744 4070 : ++p;
8745 4070 : go_assert(p->is_field_name("flags"));
8746 : // As with the other fields, the flag bits must match the reflect
8747 : // and runtime packages.
8748 4070 : unsigned long flags = 0;
8749 4070 : if (keysize > Map_type::max_key_size)
8750 19 : flags |= 1;
8751 4070 : if (valsize > Map_type::max_val_size)
8752 29 : flags |= 2;
8753 4070 : if (this->key_type_->is_reflexive())
8754 3617 : flags |= 4;
8755 4070 : if (this->key_type_->needs_key_update())
8756 2486 : flags |= 8;
8757 4070 : if (this->key_type_->hash_might_panic())
8758 16 : flags |= 16;
8759 4070 : vals->push_back(Expression::make_integer_ul(flags, uint32_type, bloc));
8760 :
8761 4070 : ++p;
8762 4070 : go_assert(p == fields->end());
8763 :
8764 4070 : return Expression::make_struct_composite_literal(mtdt, vals, bloc);
8765 : }
8766 :
8767 : // Return the bucket type to use for a map type. This must correspond
8768 : // to libgo/go/runtime/map.go.
8769 :
8770 : Type*
8771 5383 : Map_type::bucket_type(Gogo* gogo, int64_t keysize, int64_t valsize)
8772 : {
8773 5383 : if (this->bucket_type_ != NULL)
8774 : return this->bucket_type_;
8775 :
8776 4323 : Type* key_type = this->key_type_;
8777 4323 : if (keysize > Map_type::max_key_size)
8778 19 : key_type = Type::make_pointer_type(key_type);
8779 :
8780 4323 : Type* val_type = this->val_type_;
8781 4323 : if (valsize > Map_type::max_val_size)
8782 29 : val_type = Type::make_pointer_type(val_type);
8783 :
8784 4323 : Expression* bucket_size = Expression::make_integer_ul(Map_type::bucket_size,
8785 : NULL, this->location_);
8786 :
8787 4323 : Type* uint8_type = Type::lookup_integer_type("uint8");
8788 4323 : Array_type* topbits_type = Type::make_array_type(uint8_type, bucket_size);
8789 4323 : topbits_type->set_is_array_incomparable();
8790 4323 : Array_type* keys_type = Type::make_array_type(key_type, bucket_size);
8791 4323 : keys_type->set_is_array_incomparable();
8792 4323 : Array_type* values_type = Type::make_array_type(val_type, bucket_size);
8793 4323 : values_type->set_is_array_incomparable();
8794 :
8795 : // If keys and values have no pointers, the map implementation can
8796 : // keep a list of overflow pointers on the side so that buckets can
8797 : // be marked as having no pointers. Arrange for the bucket to have
8798 : // no pointers by changing the type of the overflow field to uintptr
8799 : // in this case. See comment on the hmap.overflow field in
8800 : // libgo/go/runtime/map.go.
8801 4323 : Type* overflow_type;
8802 5388 : if (!key_type->has_pointer() && !val_type->has_pointer())
8803 432 : overflow_type = Type::lookup_integer_type("uintptr");
8804 : else
8805 : {
8806 : // This should really be a pointer to the bucket type itself,
8807 : // but that would require us to construct a Named_type for it to
8808 : // give it a way to refer to itself. Since nothing really cares
8809 : // (except perhaps for someone using a debugger) just use an
8810 : // unsafe pointer.
8811 3891 : overflow_type = Type::make_pointer_type(Type::make_void_type());
8812 : }
8813 :
8814 : // Make sure the overflow pointer is the last memory in the struct,
8815 : // because the runtime assumes it can use size-ptrSize as the offset
8816 : // of the overflow pointer. We double-check that property below
8817 : // once the offsets and size are computed.
8818 :
8819 4323 : int64_t topbits_field_size, topbits_field_align;
8820 4323 : int64_t keys_field_size, keys_field_align;
8821 4323 : int64_t values_field_size, values_field_align;
8822 4323 : int64_t overflow_field_size, overflow_field_align;
8823 4323 : if (!topbits_type->backend_type_size(gogo, &topbits_field_size)
8824 4323 : || !topbits_type->backend_type_field_align(gogo, &topbits_field_align)
8825 4323 : || !keys_type->backend_type_size(gogo, &keys_field_size)
8826 4323 : || !keys_type->backend_type_field_align(gogo, &keys_field_align)
8827 4323 : || !values_type->backend_type_size(gogo, &values_field_size)
8828 4323 : || !values_type->backend_type_field_align(gogo, &values_field_align)
8829 4323 : || !overflow_type->backend_type_size(gogo, &overflow_field_size)
8830 8646 : || !overflow_type->backend_type_field_align(gogo, &overflow_field_align))
8831 : {
8832 0 : go_assert(saw_errors());
8833 : return NULL;
8834 : }
8835 :
8836 4323 : Struct_type* ret;
8837 4323 : int64_t max_align = std::max(std::max(topbits_field_align, keys_field_align),
8838 : values_field_align);
8839 4323 : if (max_align <= overflow_field_align)
8840 4323 : ret = make_builtin_struct_type(4,
8841 : "topbits", topbits_type,
8842 : "keys", keys_type,
8843 : "values", values_type,
8844 : "overflow", overflow_type);
8845 : else
8846 : {
8847 0 : size_t off = topbits_field_size;
8848 0 : off = ((off + keys_field_align - 1)
8849 0 : &~ static_cast<size_t>(keys_field_align - 1));
8850 0 : off += keys_field_size;
8851 0 : off = ((off + values_field_align - 1)
8852 0 : &~ static_cast<size_t>(values_field_align - 1));
8853 0 : off += values_field_size;
8854 :
8855 0 : int64_t padded_overflow_field_size =
8856 0 : ((overflow_field_size + max_align - 1)
8857 0 : &~ static_cast<size_t>(max_align - 1));
8858 :
8859 0 : size_t ovoff = off;
8860 0 : ovoff = ((ovoff + max_align - 1)
8861 0 : &~ static_cast<size_t>(max_align - 1));
8862 0 : size_t pad = (ovoff - off
8863 0 : + padded_overflow_field_size - overflow_field_size);
8864 :
8865 0 : Expression* pad_expr = Expression::make_integer_ul(pad, NULL,
8866 : this->location_);
8867 0 : Array_type* pad_type = Type::make_array_type(uint8_type, pad_expr);
8868 0 : pad_type->set_is_array_incomparable();
8869 :
8870 0 : ret = make_builtin_struct_type(5,
8871 : "topbits", topbits_type,
8872 : "keys", keys_type,
8873 : "values", values_type,
8874 : "pad", pad_type,
8875 : "overflow", overflow_type);
8876 : }
8877 :
8878 : // Verify that the overflow field is just before the end of the
8879 : // bucket type.
8880 :
8881 4323 : Btype* btype = ret->get_backend(gogo);
8882 4323 : int64_t offset = gogo->backend()->type_field_offset(btype,
8883 4323 : ret->field_count() - 1);
8884 4323 : int64_t size;
8885 4323 : if (!ret->backend_type_size(gogo, &size))
8886 : {
8887 0 : go_assert(saw_errors());
8888 : return NULL;
8889 : }
8890 :
8891 4323 : int64_t ptr_size;
8892 4323 : if (!Type::make_pointer_type(uint8_type)->backend_type_size(gogo, &ptr_size))
8893 : {
8894 0 : go_assert(saw_errors());
8895 : return NULL;
8896 : }
8897 :
8898 4323 : go_assert(offset + ptr_size == size);
8899 :
8900 4323 : ret->set_is_struct_incomparable();
8901 :
8902 4323 : this->bucket_type_ = ret;
8903 4323 : return ret;
8904 : }
8905 :
8906 : // Return the hashmap type for a map type.
8907 :
8908 : Type*
8909 1313 : Map_type::hmap_type(Type* bucket_type)
8910 : {
8911 1313 : if (this->hmap_type_ != NULL)
8912 : return this->hmap_type_;
8913 :
8914 1313 : Type* int_type = Type::lookup_integer_type("int");
8915 1313 : Type* uint8_type = Type::lookup_integer_type("uint8");
8916 1313 : Type* uint16_type = Type::lookup_integer_type("uint16");
8917 1313 : Type* uint32_type = Type::lookup_integer_type("uint32");
8918 1313 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
8919 1313 : Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8920 :
8921 1313 : Type* ptr_bucket_type = Type::make_pointer_type(bucket_type);
8922 :
8923 1313 : Struct_type* ret = make_builtin_struct_type(9,
8924 : "count", int_type,
8925 : "flags", uint8_type,
8926 : "B", uint8_type,
8927 : "noverflow", uint16_type,
8928 : "hash0", uint32_type,
8929 : "buckets", ptr_bucket_type,
8930 : "oldbuckets", ptr_bucket_type,
8931 : "nevacuate", uintptr_type,
8932 : "extra", void_ptr_type);
8933 1313 : ret->set_is_struct_incomparable();
8934 1313 : this->hmap_type_ = ret;
8935 1313 : return ret;
8936 : }
8937 :
8938 : // Return the iterator type for a map type. This is the type of the
8939 : // value used when doing a range over a map.
8940 :
8941 : Type*
8942 2034 : Map_type::hiter_type(Gogo* gogo)
8943 : {
8944 2034 : if (this->hiter_type_ != NULL)
8945 : return this->hiter_type_;
8946 :
8947 1313 : int64_t keysize, valsize;
8948 1313 : if (!this->key_type_->backend_type_size(gogo, &keysize)
8949 1313 : || !this->val_type_->backend_type_size(gogo, &valsize))
8950 : {
8951 0 : go_assert(saw_errors());
8952 : return NULL;
8953 : }
8954 :
8955 1313 : Type* key_ptr_type = Type::make_pointer_type(this->key_type_);
8956 1313 : Type* val_ptr_type = Type::make_pointer_type(this->val_type_);
8957 1313 : Type* uint8_type = Type::lookup_integer_type("uint8");
8958 1313 : Type* uint8_ptr_type = Type::make_pointer_type(uint8_type);
8959 1313 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
8960 1313 : Type* bucket_type = this->bucket_type(gogo, keysize, valsize);
8961 1313 : Type* bucket_ptr_type = Type::make_pointer_type(bucket_type);
8962 1313 : Type* hmap_type = this->hmap_type(bucket_type);
8963 1313 : Type* hmap_ptr_type = Type::make_pointer_type(hmap_type);
8964 1313 : Type* void_ptr_type = Type::make_pointer_type(Type::make_void_type());
8965 1313 : Type* bool_type = Type::lookup_bool_type();
8966 :
8967 1313 : Struct_type* ret = make_builtin_struct_type(15,
8968 : "key", key_ptr_type,
8969 : "val", val_ptr_type,
8970 : "t", uint8_ptr_type,
8971 : "h", hmap_ptr_type,
8972 : "buckets", bucket_ptr_type,
8973 : "bptr", bucket_ptr_type,
8974 : "overflow", void_ptr_type,
8975 : "oldoverflow", void_ptr_type,
8976 : "startBucket", uintptr_type,
8977 : "offset", uint8_type,
8978 : "wrapped", bool_type,
8979 : "B", uint8_type,
8980 : "i", uint8_type,
8981 : "bucket", uintptr_type,
8982 : "checkBucket", uintptr_type);
8983 1313 : ret->set_is_struct_incomparable();
8984 1313 : this->hiter_type_ = ret;
8985 1313 : return ret;
8986 : }
8987 :
8988 : // Reflection string for a map.
8989 :
8990 : void
8991 5463 : Map_type::do_reflection(Gogo* gogo, std::string* ret) const
8992 : {
8993 5463 : ret->append("map[");
8994 5463 : this->append_reflection(this->key_type_, gogo, ret);
8995 5463 : ret->append("]");
8996 5463 : this->append_reflection(this->val_type_, gogo, ret);
8997 5463 : }
8998 :
8999 : // Export a map type.
9000 :
9001 : void
9002 6059 : Map_type::do_export(Export* exp) const
9003 : {
9004 6059 : exp->write_c_string("map [");
9005 6059 : exp->write_type(this->key_type_);
9006 6059 : exp->write_c_string("] ");
9007 6059 : exp->write_type(this->val_type_);
9008 6059 : }
9009 :
9010 : // Import a map type.
9011 :
9012 : Map_type*
9013 21174 : Map_type::do_import(Import* imp)
9014 : {
9015 21174 : imp->require_c_string("map [");
9016 21174 : Type* key_type = imp->read_type();
9017 21174 : imp->require_c_string("] ");
9018 21174 : Type* val_type = imp->read_type();
9019 21174 : return Type::make_map_type(key_type, val_type, imp->location());
9020 : }
9021 :
9022 : // Make a map type.
9023 :
9024 : Map_type*
9025 32050 : Type::make_map_type(Type* key_type, Type* val_type, Location location)
9026 : {
9027 32050 : return new Map_type(key_type, val_type, location);
9028 : }
9029 :
9030 : // Class Channel_type.
9031 :
9032 : // Message name.
9033 :
9034 : void
9035 29 : Channel_type::do_message_name(std::string* ret) const
9036 : {
9037 29 : if (!this->may_send_)
9038 5 : ret->append("<-");
9039 29 : ret->append("chan");
9040 29 : if (!this->may_receive_)
9041 5 : ret->append("<-");
9042 29 : ret->push_back(' ');
9043 29 : this->append_message_name(this->element_type_, ret);
9044 29 : }
9045 :
9046 : // Verify.
9047 :
9048 : bool
9049 27823 : Channel_type::do_verify(Gogo*)
9050 : {
9051 : // We have no location for this error, but this is not something the
9052 : // ordinary user will see.
9053 27823 : if (!this->element_type_->in_heap())
9054 : {
9055 0 : go_error_at(Linemap::unknown_location(),
9056 : "chan of go:notinheap type not allowed");
9057 0 : this->set_is_error();
9058 : }
9059 27823 : return true;
9060 : }
9061 :
9062 : // Hash code.
9063 :
9064 : unsigned int
9065 64291 : Channel_type::do_hash_for_method(Gogo* gogo, int flags) const
9066 : {
9067 64291 : unsigned int ret = 0;
9068 64291 : if (this->may_send_)
9069 54698 : ret += 1;
9070 64291 : if (this->may_receive_)
9071 60162 : ret += 2;
9072 64291 : if (this->element_type_ != NULL)
9073 64291 : ret += this->element_type_->hash_for_method(gogo, flags) << 2;
9074 64291 : return ret << 3;
9075 : }
9076 :
9077 : // Whether this type is the same as T.
9078 :
9079 : bool
9080 26351 : Channel_type::is_identical(const Channel_type* t, int flags) const
9081 : {
9082 26351 : if (!Type::are_identical(this->element_type(), t->element_type(), flags,
9083 : NULL))
9084 : return false;
9085 26319 : return (this->may_send_ == t->may_send_
9086 26319 : && this->may_receive_ == t->may_receive_);
9087 : }
9088 :
9089 : // Return the backend representation for a channel type. A channel is a pointer
9090 : // to a __go_channel struct. The __go_channel struct is defined in
9091 : // libgo/runtime/channel.h.
9092 :
9093 : Btype*
9094 8617 : Channel_type::do_get_backend(Gogo* gogo)
9095 : {
9096 8617 : static Btype* backend_channel_type;
9097 8617 : if (backend_channel_type == NULL)
9098 : {
9099 1976 : std::vector<Backend::Btyped_identifier> bfields;
9100 1976 : Btype* bt = gogo->backend()->struct_type(bfields);
9101 1976 : bt = gogo->backend()->named_type("__go_channel", bt,
9102 : Linemap::predeclared_location());
9103 1976 : backend_channel_type = gogo->backend()->pointer_type(bt);
9104 1976 : }
9105 8617 : return backend_channel_type;
9106 : }
9107 :
9108 : // Build a type descriptor for a channel type.
9109 :
9110 : Type*
9111 5902 : Channel_type::make_chan_type_descriptor_type()
9112 : {
9113 5902 : static Type* ret;
9114 5902 : if (ret == NULL)
9115 : {
9116 4646 : Type* tdt = Type::make_type_descriptor_type();
9117 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
9118 :
9119 4646 : Type* uintptr_type = Type::lookup_integer_type("uintptr");
9120 :
9121 4646 : Struct_type* sf =
9122 4646 : Type::make_builtin_struct_type(3,
9123 : "", tdt,
9124 : "elem", ptdt,
9125 : "dir", uintptr_type);
9126 :
9127 4646 : ret = Type::make_builtin_named_type("ChanType", sf);
9128 : }
9129 :
9130 5902 : return ret;
9131 : }
9132 :
9133 : // Build a type descriptor for a map type.
9134 :
9135 : Expression*
9136 1256 : Channel_type::do_type_descriptor(Gogo* gogo, Named_type* name)
9137 : {
9138 1256 : Location bloc = Linemap::predeclared_location();
9139 :
9140 1256 : Type* ctdt = Channel_type::make_chan_type_descriptor_type();
9141 :
9142 2512 : const Struct_field_list* fields = ctdt->struct_type()->fields();
9143 :
9144 1256 : Expression_list* vals = new Expression_list();
9145 1256 : vals->reserve(3);
9146 :
9147 1256 : Struct_field_list::const_iterator p = fields->begin();
9148 1256 : go_assert(p->is_field_name("_type"));
9149 1256 : vals->push_back(this->type_descriptor_constructor(gogo,
9150 : RUNTIME_TYPE_KIND_CHAN,
9151 : name, NULL, true));
9152 :
9153 1256 : ++p;
9154 1256 : go_assert(p->is_field_name("elem"));
9155 1256 : vals->push_back(Expression::make_type_descriptor(this->element_type_, bloc));
9156 :
9157 1256 : ++p;
9158 1256 : go_assert(p->is_field_name("dir"));
9159 : // These bits must match the ones in libgo/runtime/go-type.h.
9160 1256 : int val = 0;
9161 1256 : if (this->may_receive_)
9162 1141 : val |= 1;
9163 1256 : if (this->may_send_)
9164 1122 : val |= 2;
9165 1256 : vals->push_back(Expression::make_integer_ul(val, p->type(), bloc));
9166 :
9167 1256 : ++p;
9168 1256 : go_assert(p == fields->end());
9169 :
9170 1256 : return Expression::make_struct_composite_literal(ctdt, vals, bloc);
9171 : }
9172 :
9173 : // Reflection string.
9174 :
9175 : void
9176 4227 : Channel_type::do_reflection(Gogo* gogo, std::string* ret) const
9177 : {
9178 4227 : if (!this->may_send_)
9179 452 : ret->append("<-");
9180 4227 : ret->append("chan");
9181 4227 : if (!this->may_receive_)
9182 279 : ret->append("<-");
9183 4227 : ret->push_back(' ');
9184 :
9185 4227 : bool need_paren = false;
9186 4227 : if (this->may_send_
9187 3775 : && this->may_receive_
9188 3496 : && this->element_type_->channel_type() != NULL
9189 20 : && this->element_type_->unalias()->named_type() == NULL
9190 4265 : && !this->element_type_->channel_type()->may_send())
9191 : {
9192 7 : ret->push_back('(');
9193 7 : need_paren = true;
9194 : }
9195 :
9196 4227 : this->append_reflection(this->element_type_, gogo, ret);
9197 :
9198 4227 : if (need_paren)
9199 7 : ret->push_back(')');
9200 4227 : }
9201 :
9202 : // Export.
9203 :
9204 : void
9205 1500 : Channel_type::do_export(Export* exp) const
9206 : {
9207 1500 : exp->write_c_string("chan ");
9208 1500 : if (this->may_send_ && !this->may_receive_)
9209 79 : exp->write_c_string("-< ");
9210 1421 : else if (this->may_receive_ && !this->may_send_)
9211 435 : exp->write_c_string("<- ");
9212 1500 : exp->write_type(this->element_type_);
9213 1500 : }
9214 :
9215 : // Import.
9216 :
9217 : Channel_type*
9218 9122 : Channel_type::do_import(Import* imp)
9219 : {
9220 9122 : imp->require_c_string("chan ");
9221 :
9222 9122 : bool may_send;
9223 9122 : bool may_receive;
9224 9122 : if (imp->match_c_string("-< "))
9225 : {
9226 381 : imp->advance(3);
9227 381 : may_send = true;
9228 381 : may_receive = false;
9229 : }
9230 8741 : else if (imp->match_c_string("<- "))
9231 : {
9232 2856 : imp->advance(3);
9233 2856 : may_receive = true;
9234 2856 : may_send = false;
9235 : }
9236 : else
9237 : {
9238 : may_send = true;
9239 : may_receive = true;
9240 : }
9241 :
9242 9122 : Type* element_type = imp->read_type();
9243 :
9244 9122 : return Type::make_channel_type(may_send, may_receive, element_type);
9245 : }
9246 :
9247 : // Return the type that the runtime package uses for one case of a
9248 : // select statement. An array of values of this type is allocated on
9249 : // the stack. This must match scase in libgo/go/runtime/select.go.
9250 :
9251 : Type*
9252 1951 : Channel_type::select_case_type()
9253 : {
9254 1951 : static Struct_type* scase_type;
9255 1951 : if (scase_type == NULL)
9256 : {
9257 204 : Type* unsafe_pointer_type =
9258 204 : Type::make_pointer_type(Type::make_void_type());
9259 408 : scase_type =
9260 204 : Type::make_builtin_struct_type(2,
9261 : "c", unsafe_pointer_type,
9262 : "elem", unsafe_pointer_type);
9263 204 : scase_type->set_is_struct_incomparable();
9264 : }
9265 1951 : return scase_type;
9266 : }
9267 :
9268 : // Make a new channel type.
9269 :
9270 : Channel_type*
9271 14400 : Type::make_channel_type(bool send, bool receive, Type* element_type)
9272 : {
9273 14400 : return new Channel_type(send, receive, element_type);
9274 : }
9275 :
9276 : // Class Interface_type.
9277 :
9278 : // Return the list of methods.
9279 :
9280 : const Typed_identifier_list*
9281 419219 : Interface_type::methods() const
9282 : {
9283 419219 : go_assert(this->methods_are_finalized_ || saw_errors());
9284 419219 : return this->all_methods_;
9285 : }
9286 :
9287 : // Return the number of methods.
9288 :
9289 : size_t
9290 0 : Interface_type::method_count() const
9291 : {
9292 0 : go_assert(this->methods_are_finalized_ || saw_errors());
9293 0 : return this->all_methods_ == NULL ? 0 : this->all_methods_->size();
9294 : }
9295 :
9296 : // Message name.
9297 :
9298 : void
9299 41 : Interface_type::do_message_name(std::string* ret) const
9300 : {
9301 41 : const Typed_identifier_list* methods = (this->methods_are_finalized_
9302 : ? this->all_methods_
9303 : : this->parse_methods_);
9304 41 : if (methods == NULL || methods->empty())
9305 : {
9306 2 : ret->append("interface{}");
9307 2 : return;
9308 : }
9309 :
9310 39 : ret->append("interface {");
9311 :
9312 39 : bool first = true;
9313 78 : for (Typed_identifier_list::const_iterator p = methods->begin();
9314 78 : p != methods->end();
9315 39 : ++p)
9316 : {
9317 39 : if (first)
9318 : first = false;
9319 : else
9320 0 : ret->append("; ");
9321 :
9322 39 : if (!p->name().empty())
9323 39 : ret->append(p->name());
9324 :
9325 39 : Function_type* ft = p->type()->function_type();
9326 39 : if (ft == NULL)
9327 0 : this->append_message_name(p->type(), ret);
9328 : else
9329 39 : ft->append_signature(ret);
9330 : }
9331 :
9332 39 : ret->append(" }");
9333 : }
9334 :
9335 : // Traversal.
9336 :
9337 : int
9338 13175554 : Interface_type::do_traverse(Traverse* traverse)
9339 : {
9340 13175554 : Typed_identifier_list* methods = (this->methods_are_finalized_
9341 13175554 : ? this->all_methods_
9342 : : this->parse_methods_);
9343 13175554 : if (methods == NULL)
9344 : return TRAVERSE_CONTINUE;
9345 11255939 : return methods->traverse(traverse);
9346 : }
9347 :
9348 : // Finalize the methods. This handles interface inheritance.
9349 :
9350 : void
9351 328434 : Interface_type::finalize_methods()
9352 : {
9353 328434 : if (this->methods_are_finalized_)
9354 255447 : return;
9355 123312 : this->methods_are_finalized_ = true;
9356 123312 : if (this->parse_methods_ == NULL)
9357 : return;
9358 :
9359 : // The exporter uses parse_methods_.
9360 72987 : this->parse_methods_->sort_by_name();
9361 :
9362 72987 : this->all_methods_ = new Typed_identifier_list();
9363 72987 : this->all_methods_->reserve(this->parse_methods_->size());
9364 72987 : Typed_identifier_list inherit;
9365 229535 : for (Typed_identifier_list::const_iterator pm =
9366 72987 : this->parse_methods_->begin();
9367 302522 : pm != this->parse_methods_->end();
9368 229535 : ++pm)
9369 : {
9370 229535 : const Typed_identifier* p = &*pm;
9371 229535 : if (p->name().empty())
9372 26932 : inherit.push_back(*p);
9373 202603 : else if (this->find_method(p->name()) == NULL)
9374 202602 : this->all_methods_->push_back(*p);
9375 : else
9376 : {
9377 1 : go_error_at(p->location(), "duplicate method %qs",
9378 1 : Gogo::message_name(p->name()).c_str());
9379 1 : this->set_is_error();
9380 : }
9381 : }
9382 :
9383 72987 : std::vector<Named_type*> seen;
9384 72987 : seen.reserve(inherit.size());
9385 72987 : bool issued_recursive_error = false;
9386 173240 : while (!inherit.empty())
9387 : {
9388 27266 : Type* t = inherit.back().type();
9389 27266 : Location tl = inherit.back().location();
9390 27266 : inherit.pop_back();
9391 :
9392 27266 : Interface_type* it = t->interface_type();
9393 27275 : if (it == NULL)
9394 : {
9395 9 : if (!t->is_error())
9396 : {
9397 6 : go_error_at(tl, "interface contains embedded non-interface");
9398 6 : this->set_is_error();
9399 : }
9400 24 : continue;
9401 : }
9402 27257 : if (it == this)
9403 : {
9404 4 : if (!issued_recursive_error)
9405 : {
9406 4 : go_error_at(tl, "invalid recursive interface");
9407 4 : this->set_is_error();
9408 4 : issued_recursive_error = true;
9409 : }
9410 4 : continue;
9411 : }
9412 :
9413 27253 : const Typed_identifier_list* imethods = it->parse_methods_;
9414 27253 : if (imethods == NULL)
9415 2 : continue;
9416 :
9417 27251 : Named_type* nt = t->named_type();
9418 27251 : if (nt != NULL)
9419 : {
9420 27251 : std::vector<Named_type*>::const_iterator q;
9421 27251 : for (q = seen.begin(); q != seen.end(); ++q)
9422 : {
9423 0 : if (*q == nt)
9424 : {
9425 0 : go_error_at(tl, "inherited interface loop");
9426 0 : this->set_is_error();
9427 0 : break;
9428 : }
9429 : }
9430 27251 : if (q != seen.end())
9431 0 : continue;
9432 27251 : seen.push_back(nt);
9433 : }
9434 :
9435 59120 : for (Typed_identifier_list::const_iterator q = imethods->begin();
9436 59120 : q != imethods->end();
9437 31869 : ++q)
9438 : {
9439 31869 : if (q->name().empty())
9440 334 : inherit.push_back(*q);
9441 : else
9442 : {
9443 31535 : const Typed_identifier* oldm = this->find_method(q->name());
9444 31535 : if (oldm == NULL)
9445 31459 : this->all_methods_->push_back(Typed_identifier(q->name(),
9446 62918 : q->type(), tl));
9447 76 : else if (!Type::are_identical(q->type(), oldm->type(),
9448 : Type::COMPARE_TAGS, NULL))
9449 : {
9450 3 : go_error_at(tl, "duplicate method %qs",
9451 3 : Gogo::message_name(q->name()).c_str());
9452 3 : this->set_is_error();
9453 : }
9454 : }
9455 : }
9456 :
9457 27251 : seen.pop_back();
9458 : }
9459 :
9460 72987 : if (!this->all_methods_->empty())
9461 72974 : this->all_methods_->sort_by_name();
9462 : else
9463 : {
9464 13 : delete this->all_methods_;
9465 13 : this->all_methods_ = NULL;
9466 : }
9467 72987 : }
9468 :
9469 : // Return the method NAME, or NULL.
9470 :
9471 : const Typed_identifier*
9472 1762294 : Interface_type::find_method(const std::string& name) const
9473 : {
9474 1762294 : go_assert(this->methods_are_finalized_);
9475 1762294 : if (this->all_methods_ == NULL)
9476 : return NULL;
9477 7357374 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9478 7357374 : p != this->all_methods_->end();
9479 5595135 : ++p)
9480 7120250 : if (p->name() == name)
9481 1762294 : return &*p;
9482 : return NULL;
9483 : }
9484 :
9485 : // Return the method index.
9486 :
9487 : size_t
9488 0 : Interface_type::method_index(const std::string& name) const
9489 : {
9490 0 : go_assert(this->methods_are_finalized_ && this->all_methods_ != NULL);
9491 0 : size_t ret = 0;
9492 0 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9493 0 : p != this->all_methods_->end();
9494 0 : ++p, ++ret)
9495 0 : if (p->name() == name)
9496 0 : return ret;
9497 0 : go_unreachable();
9498 : }
9499 :
9500 : // Return whether NAME is an unexported method, for better error
9501 : // reporting.
9502 :
9503 : bool
9504 3 : Interface_type::is_unexported_method(Gogo* gogo, const std::string& name) const
9505 : {
9506 3 : go_assert(this->methods_are_finalized_);
9507 3 : if (this->all_methods_ == NULL)
9508 : return false;
9509 6 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9510 6 : p != this->all_methods_->end();
9511 3 : ++p)
9512 : {
9513 5 : const std::string& method_name(p->name());
9514 10 : if (Gogo::is_hidden_name(method_name)
9515 8 : && name == Gogo::unpack_hidden_name(method_name)
9516 10 : && gogo->pack_hidden_name(name, false) != method_name)
9517 3 : return true;
9518 : }
9519 : return false;
9520 : }
9521 :
9522 : // Whether this type is identical with T.
9523 :
9524 : bool
9525 110154 : Interface_type::is_identical(const Interface_type* t, int flags) const
9526 : {
9527 : // If methods have not been finalized, then we are asking whether
9528 : // func redeclarations are the same. This is an error, so for
9529 : // simplicity we say they are never the same.
9530 110154 : if (!this->methods_are_finalized_ || !t->methods_are_finalized_)
9531 : return false;
9532 :
9533 : // Consult a flag to see whether we need to compare based on
9534 : // parse methods or all methods.
9535 110154 : Typed_identifier_list* methods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
9536 : ? this->parse_methods_
9537 : : this->all_methods_);
9538 110154 : Typed_identifier_list* tmethods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
9539 : ? t->parse_methods_
9540 : : t->all_methods_);
9541 :
9542 : // We require the same methods with the same types. The methods
9543 : // have already been sorted.
9544 110154 : if (methods == NULL || tmethods == NULL)
9545 107497 : return methods == tmethods;
9546 :
9547 2657 : if (this->assume_identical(this, t) || t->assume_identical(t, this))
9548 24 : return true;
9549 :
9550 2633 : Assume_identical* hold_ai = this->assume_identical_;
9551 2633 : Assume_identical ai;
9552 2633 : ai.t1 = this;
9553 2633 : ai.t2 = t;
9554 2633 : ai.next = hold_ai;
9555 2633 : this->assume_identical_ = &ai;
9556 :
9557 2633 : Typed_identifier_list::const_iterator p1 = methods->begin();
9558 2633 : Typed_identifier_list::const_iterator p2;
9559 4627 : for (p2 = tmethods->begin(); p2 != tmethods->end(); ++p1, ++p2)
9560 : {
9561 3743 : if (p1 == methods->end())
9562 : break;
9563 3725 : if (p1->name() != p2->name()
9564 3725 : || !Type::are_identical(p1->type(), p2->type(), flags, NULL))
9565 : break;
9566 : }
9567 :
9568 2633 : this->assume_identical_ = hold_ai;
9569 :
9570 2633 : return p1 == methods->end() && p2 == tmethods->end();
9571 : }
9572 :
9573 : // Return true if T1 and T2 are assumed to be identical during a type
9574 : // comparison.
9575 :
9576 : bool
9577 5290 : Interface_type::assume_identical(const Interface_type* t1,
9578 : const Interface_type* t2) const
9579 : {
9580 5290 : for (Assume_identical* p = this->assume_identical_;
9581 5290 : p != NULL;
9582 0 : p = p->next)
9583 24 : if ((p->t1 == t1 && p->t2 == t2) || (p->t1 == t2 && p->t2 == t1))
9584 : return true;
9585 : return false;
9586 : }
9587 :
9588 : // Whether we can assign the interface type T to this type. The types
9589 : // are known to not be identical. An interface assignment is only
9590 : // permitted if T is known to implement all methods in THIS.
9591 : // Otherwise a type guard is required.
9592 :
9593 : bool
9594 4508 : Interface_type::is_compatible_for_assign(const Interface_type* t,
9595 : std::string* reason) const
9596 : {
9597 4508 : go_assert(this->methods_are_finalized_ && t->methods_are_finalized_);
9598 4508 : if (this->all_methods_ == NULL)
9599 : return true;
9600 4508 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9601 10775 : p != this->all_methods_->end();
9602 6267 : ++p)
9603 : {
9604 6359 : const Typed_identifier* m = t->find_method(p->name());
9605 6359 : if (m == NULL)
9606 : {
9607 88 : if (reason != NULL)
9608 : {
9609 4 : char buf[200];
9610 8 : snprintf(buf, sizeof buf,
9611 4 : _("need explicit conversion; missing method %s%s%s"),
9612 4 : go_open_quote(), Gogo::message_name(p->name()).c_str(),
9613 : go_close_quote());
9614 4 : reason->assign(buf);
9615 : }
9616 92 : return false;
9617 : }
9618 :
9619 6271 : std::string subreason;
9620 6271 : if (!Type::are_identical(p->type(), m->type(), Type::COMPARE_TAGS,
9621 : &subreason))
9622 : {
9623 4 : if (reason != NULL)
9624 : {
9625 4 : std::string n = Gogo::message_name(p->name());
9626 4 : size_t len = 100 + n.length() + subreason.length();
9627 4 : char* buf = new char[len];
9628 4 : if (subreason.empty())
9629 0 : snprintf(buf, len, _("incompatible type for method %s%s%s"),
9630 : go_open_quote(), n.c_str(), go_close_quote());
9631 : else
9632 4 : snprintf(buf, len,
9633 4 : _("incompatible type for method %s%s%s (%s)"),
9634 : go_open_quote(), n.c_str(), go_close_quote(),
9635 : subreason.c_str());
9636 4 : reason->assign(buf);
9637 4 : delete[] buf;
9638 4 : }
9639 4 : return false;
9640 : }
9641 6271 : }
9642 :
9643 : return true;
9644 : }
9645 :
9646 : // Hash code.
9647 :
9648 : unsigned int
9649 644552 : Interface_type::do_hash_for_method(Gogo*, int flags) const
9650 : {
9651 644552 : go_assert(this->methods_are_finalized_);
9652 644552 : Typed_identifier_list* methods = (((flags & COMPARE_EMBEDDED_INTERFACES) != 0)
9653 : ? this->parse_methods_
9654 : : this->all_methods_);
9655 644552 : unsigned int ret = 0;
9656 644552 : if (methods != NULL)
9657 : {
9658 329355 : for (Typed_identifier_list::const_iterator p = methods->begin();
9659 2425756 : p != methods->end();
9660 2096401 : ++p)
9661 : {
9662 2096401 : ret = Gogo::hash_string(p->name(), ret);
9663 : // We don't use the method type in the hash, to avoid
9664 : // infinite recursion if an interface method uses a type
9665 : // which is an interface which inherits from the interface
9666 : // itself.
9667 : // type T interface { F() interface {T}}
9668 2096401 : ret <<= 1;
9669 : }
9670 : }
9671 644552 : return ret;
9672 : }
9673 :
9674 : // Return true if T implements the interface. If it does not, and
9675 : // REASON is not NULL, set *REASON to a useful error message.
9676 :
9677 : bool
9678 19920476 : Interface_type::implements_interface(const Type* t, std::string* reason) const
9679 : {
9680 19920476 : go_assert(this->methods_are_finalized_);
9681 19920476 : if (this->all_methods_ == NULL)
9682 : return true;
9683 :
9684 19529288 : t = t->unalias();
9685 19529288 : bool is_pointer = false;
9686 19529288 : const Named_type* nt = t->named_type();
9687 19529288 : const Struct_type* st = t->struct_type();
9688 : // If we start with a named type, we don't dereference it to find
9689 : // methods.
9690 19529288 : if (nt == NULL)
9691 : {
9692 19491751 : const Type* pt = t->points_to();
9693 19491751 : if (pt != NULL)
9694 : {
9695 : // If T is a pointer to a named type, then we need to look at
9696 : // the type to which it points.
9697 19242295 : pt = pt->unalias();
9698 19242295 : is_pointer = true;
9699 19242295 : nt = pt->named_type();
9700 19242295 : st = pt->struct_type();
9701 : }
9702 : }
9703 :
9704 : // If we have a named type, get the methods from it rather than from
9705 : // any struct type.
9706 19491751 : if (nt != NULL)
9707 : st = NULL;
9708 :
9709 : // Only named and struct types have methods.
9710 19529288 : if (nt == NULL && st == NULL)
9711 : {
9712 859125 : if (reason != NULL)
9713 : {
9714 25889 : if (t->points_to() != NULL
9715 25889 : && t->points_to()->interface_type() != NULL)
9716 0 : reason->assign(_("pointer to interface type has no methods"));
9717 : else
9718 25889 : reason->assign(_("type has no methods"));
9719 : }
9720 859125 : return false;
9721 : }
9722 :
9723 18670163 : if (nt != NULL ? !nt->has_any_methods() : !st->has_any_methods())
9724 : {
9725 15989430 : if (reason != NULL)
9726 : {
9727 4322 : if (t->points_to() != NULL
9728 4322 : && t->points_to()->interface_type() != NULL)
9729 1 : reason->assign(_("pointer to interface type has no methods"));
9730 : else
9731 4321 : reason->assign(_("type has no methods"));
9732 : }
9733 15989430 : return false;
9734 : }
9735 :
9736 2680733 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9737 3856478 : p != this->all_methods_->end();
9738 1175745 : ++p)
9739 : {
9740 3674263 : bool is_ambiguous = false;
9741 3674263 : Method* m = (nt != NULL
9742 3674263 : ? nt->method_function(p->name(), &is_ambiguous)
9743 66461 : : st->method_function(p->name(), &is_ambiguous));
9744 3674263 : if (m == NULL)
9745 : {
9746 2491722 : if (reason != NULL)
9747 : {
9748 3 : std::string n = Gogo::message_name(p->name());
9749 3 : size_t len = n.length() + 100;
9750 3 : char* buf = new char[len];
9751 3 : if (is_ambiguous)
9752 0 : snprintf(buf, len, _("ambiguous method %s%s%s"),
9753 : go_open_quote(), n.c_str(), go_close_quote());
9754 : else
9755 3 : snprintf(buf, len, _("missing method %s%s%s"),
9756 : go_open_quote(), n.c_str(), go_close_quote());
9757 3 : reason->assign(buf);
9758 3 : delete[] buf;
9759 3 : }
9760 2498518 : return false;
9761 : }
9762 :
9763 1182541 : Function_type *p_fn_type = p->type()->function_type();
9764 1182541 : Function_type* m_fn_type = m->type()->function_type();
9765 1182541 : go_assert(p_fn_type != NULL && m_fn_type != NULL);
9766 1182541 : std::string subreason;
9767 1182541 : if (!p_fn_type->is_identical(m_fn_type, true, Type::COMPARE_TAGS,
9768 : &subreason))
9769 : {
9770 6793 : if (reason != NULL)
9771 : {
9772 4 : std::string n = Gogo::message_name(p->name());
9773 4 : size_t len = 100 + n.length() + subreason.length();
9774 4 : char* buf = new char[len];
9775 4 : if (subreason.empty())
9776 0 : snprintf(buf, len, _("incompatible type for method %s%s%s"),
9777 : go_open_quote(), n.c_str(), go_close_quote());
9778 : else
9779 4 : snprintf(buf, len,
9780 4 : _("incompatible type for method %s%s%s (%s)"),
9781 : go_open_quote(), n.c_str(), go_close_quote(),
9782 : subreason.c_str());
9783 4 : reason->assign(buf);
9784 4 : delete[] buf;
9785 4 : }
9786 6793 : return false;
9787 : }
9788 :
9789 1175748 : if (!is_pointer && !m->is_value_method())
9790 : {
9791 3 : if (reason != NULL)
9792 : {
9793 3 : std::string n = Gogo::message_name(p->name());
9794 3 : size_t len = 100 + n.length();
9795 3 : char* buf = new char[len];
9796 3 : snprintf(buf, len,
9797 3 : _("method %s%s%s requires a pointer receiver"),
9798 : go_open_quote(), n.c_str(), go_close_quote());
9799 3 : reason->assign(buf);
9800 3 : delete[] buf;
9801 3 : }
9802 3 : return false;
9803 : }
9804 :
9805 : // If the magic //go:nointerface comment was used, the method
9806 : // may not be used to implement interfaces.
9807 1175745 : if (m->nointerface())
9808 : {
9809 0 : if (reason != NULL)
9810 : {
9811 0 : std::string n = Gogo::message_name(p->name());
9812 0 : size_t len = 100 + n.length();
9813 0 : char* buf = new char[len];
9814 0 : snprintf(buf, len,
9815 0 : _("method %s%s%s is marked go:nointerface"),
9816 : go_open_quote(), n.c_str(), go_close_quote());
9817 0 : reason->assign(buf);
9818 0 : delete[] buf;
9819 0 : }
9820 0 : return false;
9821 : }
9822 1182541 : }
9823 :
9824 : return true;
9825 : }
9826 :
9827 : // Return the backend representation of the empty interface type. We
9828 : // use the same struct for all empty interfaces.
9829 :
9830 : Btype*
9831 19608 : Interface_type::get_backend_empty_interface_type(Gogo* gogo)
9832 : {
9833 19608 : static Btype* empty_interface_type;
9834 19608 : if (empty_interface_type == NULL)
9835 : {
9836 4646 : std::vector<Backend::Btyped_identifier> bfields(2);
9837 :
9838 4646 : Location bloc = Linemap::predeclared_location();
9839 :
9840 4646 : Type* pdt = Type::make_type_descriptor_ptr_type();
9841 4646 : bfields[0].name = "__type_descriptor";
9842 4646 : bfields[0].btype = pdt->get_backend(gogo);
9843 4646 : bfields[0].location = bloc;
9844 :
9845 4646 : Type* vt = Type::make_pointer_type(Type::make_void_type());
9846 4646 : bfields[1].name = "__object";
9847 4646 : bfields[1].btype = vt->get_backend(gogo);
9848 4646 : bfields[1].location = bloc;
9849 :
9850 4646 : empty_interface_type = gogo->backend()->struct_type(bfields);
9851 4646 : }
9852 19608 : return empty_interface_type;
9853 : }
9854 :
9855 : Interface_type::Bmethods_map Interface_type::bmethods_map;
9856 :
9857 : // Return a pointer to the backend representation of the method table.
9858 :
9859 : Btype*
9860 28230 : Interface_type::get_backend_methods(Gogo* gogo)
9861 : {
9862 28230 : if (this->bmethods_ != NULL && !this->bmethods_is_placeholder_)
9863 : return this->bmethods_;
9864 :
9865 21234 : std::pair<Interface_type*, Bmethods_map_entry> val;
9866 21234 : val.first = this;
9867 21234 : val.second.btype = NULL;
9868 21234 : val.second.is_placeholder = false;
9869 21234 : std::pair<Bmethods_map::iterator, bool> ins =
9870 21234 : Interface_type::bmethods_map.insert(val);
9871 21234 : if (!ins.second
9872 20019 : && ins.first->second.btype != NULL
9873 41253 : && !ins.first->second.is_placeholder)
9874 : {
9875 108 : this->bmethods_ = ins.first->second.btype;
9876 108 : this->bmethods_is_placeholder_ = false;
9877 108 : return this->bmethods_;
9878 : }
9879 :
9880 21126 : Location loc = this->location();
9881 :
9882 21126 : std::vector<Backend::Btyped_identifier>
9883 21126 : mfields(this->all_methods_->size() + 1);
9884 :
9885 21126 : Type* pdt = Type::make_type_descriptor_ptr_type();
9886 21126 : mfields[0].name = "__type_descriptor";
9887 21126 : mfields[0].btype = pdt->get_backend(gogo);
9888 21126 : mfields[0].location = loc;
9889 :
9890 21126 : std::string last_name = "";
9891 21126 : size_t i = 1;
9892 21126 : for (Typed_identifier_list::const_iterator p = this->all_methods_->begin();
9893 128713 : p != this->all_methods_->end();
9894 107587 : ++p, ++i)
9895 : {
9896 : // The type of the method in Go only includes the parameters.
9897 : // The actual method also has a receiver, which is always a
9898 : // pointer. We need to add that pointer type here in order to
9899 : // generate the correct type for the backend.
9900 107587 : Function_type* ft = p->type()->function_type();
9901 107587 : go_assert(ft->receiver() == NULL);
9902 :
9903 107587 : const Typed_identifier_list* params = ft->parameters();
9904 107587 : Typed_identifier_list* mparams = new Typed_identifier_list();
9905 107587 : if (params != NULL)
9906 44022 : mparams->reserve(params->size() + 1);
9907 107587 : Type* vt = Type::make_pointer_type(Type::make_void_type());
9908 215174 : mparams->push_back(Typed_identifier("", vt, ft->location()));
9909 107587 : if (params != NULL)
9910 : {
9911 44022 : for (Typed_identifier_list::const_iterator pp = params->begin();
9912 108038 : pp != params->end();
9913 64016 : ++pp)
9914 64016 : mparams->push_back(*pp);
9915 : }
9916 :
9917 107587 : Typed_identifier_list* mresults = (ft->results() == NULL
9918 107587 : ? NULL
9919 94391 : : ft->results()->copy());
9920 107587 : Function_type* mft = Type::make_function_type(NULL, mparams, mresults,
9921 : ft->location());
9922 :
9923 107587 : mfields[i].name = Gogo::unpack_hidden_name(p->name());
9924 107587 : mfields[i].btype = mft->get_backend_fntype(gogo);
9925 107587 : mfields[i].location = loc;
9926 :
9927 : // Sanity check: the names should be sorted.
9928 215174 : go_assert(Gogo::unpack_hidden_name(p->name())
9929 : > Gogo::unpack_hidden_name(last_name));
9930 107587 : last_name = p->name();
9931 : }
9932 :
9933 21126 : Btype* st = gogo->backend()->struct_type(mfields);
9934 21126 : Btype* ret = gogo->backend()->pointer_type(st);
9935 :
9936 21126 : if (ins.first->second.btype != NULL
9937 21126 : && ins.first->second.is_placeholder)
9938 19911 : gogo->backend()->set_placeholder_pointer_type(ins.first->second.btype,
9939 : ret);
9940 21126 : this->bmethods_ = ret;
9941 21126 : ins.first->second.btype = ret;
9942 21126 : this->bmethods_is_placeholder_ = false;
9943 21126 : ins.first->second.is_placeholder = false;
9944 21126 : return ret;
9945 21126 : }
9946 :
9947 : // Return a placeholder for the pointer to the backend methods table.
9948 :
9949 : Btype*
9950 74610 : Interface_type::get_backend_methods_placeholder(Gogo* gogo)
9951 : {
9952 74610 : if (this->bmethods_ == NULL)
9953 : {
9954 71698 : std::pair<Interface_type*, Bmethods_map_entry> val;
9955 71698 : val.first = this;
9956 71698 : val.second.btype = NULL;
9957 71698 : val.second.is_placeholder = false;
9958 71698 : std::pair<Bmethods_map::iterator, bool> ins =
9959 71698 : Interface_type::bmethods_map.insert(val);
9960 71698 : if (!ins.second && ins.first->second.btype != NULL)
9961 : {
9962 180 : this->bmethods_ = ins.first->second.btype;
9963 180 : this->bmethods_is_placeholder_ = ins.first->second.is_placeholder;
9964 180 : return this->bmethods_;
9965 : }
9966 :
9967 71518 : Location loc = this->location();
9968 71518 : Btype* bt = gogo->backend()->placeholder_pointer_type("", loc, false);
9969 71518 : this->bmethods_ = bt;
9970 71518 : ins.first->second.btype = bt;
9971 71518 : this->bmethods_is_placeholder_ = true;
9972 71518 : ins.first->second.is_placeholder = true;
9973 : }
9974 74430 : return this->bmethods_;
9975 : }
9976 :
9977 : // Return the fields of a non-empty interface type. This is not
9978 : // declared in types.h so that types.h doesn't have to #include
9979 : // backend.h.
9980 :
9981 : static void
9982 82775 : get_backend_interface_fields(Gogo* gogo, Interface_type* type,
9983 : bool use_placeholder,
9984 : std::vector<Backend::Btyped_identifier>* bfields)
9985 : {
9986 82775 : Location loc = type->location();
9987 :
9988 82775 : bfields->resize(2);
9989 :
9990 82775 : (*bfields)[0].name = "__methods";
9991 82775 : (*bfields)[0].btype = (use_placeholder
9992 82775 : ? type->get_backend_methods_placeholder(gogo)
9993 8165 : : type->get_backend_methods(gogo));
9994 82775 : (*bfields)[0].location = loc;
9995 :
9996 82775 : Type* vt = Type::make_pointer_type(Type::make_void_type());
9997 82775 : (*bfields)[1].name = "__object";
9998 82775 : (*bfields)[1].btype = vt->get_backend(gogo);
9999 82775 : (*bfields)[1].location = Linemap::predeclared_location();
10000 82775 : }
10001 :
10002 : // Return the backend representation for an interface type. An interface is a
10003 : // pointer to a struct. The struct has three fields. The first field is a
10004 : // pointer to the type descriptor for the dynamic type of the object.
10005 : // The second field is a pointer to a table of methods for the
10006 : // interface to be used with the object. The third field is the value
10007 : // of the object itself.
10008 :
10009 : Btype*
10010 11828 : Interface_type::do_get_backend(Gogo* gogo)
10011 : {
10012 11828 : if (this->is_empty())
10013 3661 : return Interface_type::get_backend_empty_interface_type(gogo);
10014 : else
10015 : {
10016 8167 : if (this->interface_btype_ != NULL)
10017 : return this->interface_btype_;
10018 16330 : this->interface_btype_ =
10019 8165 : gogo->backend()->placeholder_struct_type("", this->location_);
10020 8165 : std::vector<Backend::Btyped_identifier> bfields;
10021 8165 : get_backend_interface_fields(gogo, this, false, &bfields);
10022 8165 : if (!gogo->backend()->set_placeholder_struct_type(this->interface_btype_,
10023 : bfields))
10024 0 : this->interface_btype_ = gogo->backend()->error_type();
10025 8165 : return this->interface_btype_;
10026 8165 : }
10027 : }
10028 :
10029 : // Finish the backend representation of the methods.
10030 :
10031 : void
10032 20360 : Interface_type::finish_backend_methods(Gogo* gogo)
10033 : {
10034 20360 : if (!this->is_empty())
10035 : {
10036 20065 : const Typed_identifier_list* methods = this->methods();
10037 20065 : if (methods != NULL)
10038 : {
10039 126881 : for (Typed_identifier_list::const_iterator p = methods->begin();
10040 126881 : p != methods->end();
10041 106816 : ++p)
10042 106816 : p->type()->get_backend(gogo);
10043 : }
10044 :
10045 : // Getting the backend methods now will set the placeholder
10046 : // pointer.
10047 20065 : this->get_backend_methods(gogo);
10048 : }
10049 20360 : }
10050 :
10051 : // The type of an interface type descriptor.
10052 :
10053 : Type*
10054 12505 : Interface_type::make_interface_type_descriptor_type()
10055 : {
10056 12505 : static Type* ret;
10057 12505 : if (ret == NULL)
10058 : {
10059 4646 : Type* tdt = Type::make_type_descriptor_type();
10060 4646 : Type* ptdt = Type::make_type_descriptor_ptr_type();
10061 :
10062 4646 : Type* string_type = Type::lookup_string_type();
10063 4646 : Type* pointer_string_type = Type::make_pointer_type(string_type);
10064 :
10065 4646 : Struct_type* sm =
10066 4646 : Type::make_builtin_struct_type(3,
10067 : "name", pointer_string_type,
10068 : "pkgPath", pointer_string_type,
10069 : "typ", ptdt);
10070 :
10071 4646 : Type* nsm = Type::make_builtin_named_type("imethod", sm);
10072 :
10073 4646 : Type* slice_nsm = Type::make_array_type(nsm, NULL);
10074 :
10075 4646 : Struct_type* s = Type::make_builtin_struct_type(2,
10076 : "", tdt,
10077 : "methods", slice_nsm);
10078 :
10079 4646 : ret = Type::make_builtin_named_type("InterfaceType", s);
10080 : }
10081 :
10082 12505 : return ret;
10083 : }
10084 :
10085 : // Build a type descriptor for an interface type.
10086 :
10087 : Expression*
10088 7859 : Interface_type::do_type_descriptor(Gogo* gogo, Named_type* name)
10089 : {
10090 7859 : Location bloc = Linemap::predeclared_location();
10091 :
10092 7859 : Type* itdt = Interface_type::make_interface_type_descriptor_type();
10093 :
10094 15718 : const Struct_field_list* ifields = itdt->struct_type()->fields();
10095 :
10096 7859 : Expression_list* ivals = new Expression_list();
10097 7859 : ivals->reserve(2);
10098 :
10099 7859 : Struct_field_list::const_iterator pif = ifields->begin();
10100 7859 : go_assert(pif->is_field_name("_type"));
10101 7859 : const int rt = RUNTIME_TYPE_KIND_INTERFACE;
10102 7859 : ivals->push_back(this->type_descriptor_constructor(gogo, rt, name, NULL,
10103 : true));
10104 :
10105 7859 : ++pif;
10106 7859 : go_assert(pif->is_field_name("methods"));
10107 :
10108 7859 : Expression_list* methods = new Expression_list();
10109 7859 : if (this->all_methods_ != NULL)
10110 : {
10111 13246 : Type* elemtype = pif->type()->array_type()->element_type();
10112 :
10113 6623 : methods->reserve(this->all_methods_->size());
10114 24005 : for (Typed_identifier_list::const_iterator pm =
10115 6623 : this->all_methods_->begin();
10116 24005 : pm != this->all_methods_->end();
10117 17382 : ++pm)
10118 : {
10119 34764 : const Struct_field_list* mfields = elemtype->struct_type()->fields();
10120 :
10121 17382 : Expression_list* mvals = new Expression_list();
10122 17382 : mvals->reserve(3);
10123 :
10124 17382 : Struct_field_list::const_iterator pmf = mfields->begin();
10125 17382 : go_assert(pmf->is_field_name("name"));
10126 17382 : std::string s = Gogo::unpack_hidden_name(pm->name());
10127 17382 : Expression* e = Expression::make_string(s, bloc);
10128 17382 : mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
10129 :
10130 17382 : ++pmf;
10131 17382 : go_assert(pmf->is_field_name("pkgPath"));
10132 17382 : if (!Gogo::is_hidden_name(pm->name()))
10133 15429 : mvals->push_back(Expression::make_nil(bloc));
10134 : else
10135 : {
10136 1953 : s = Gogo::hidden_name_pkgpath(pm->name());
10137 1953 : e = Expression::make_string(s, bloc);
10138 1953 : mvals->push_back(Expression::make_unary(OPERATOR_AND, e, bloc));
10139 : }
10140 :
10141 17382 : ++pmf;
10142 17382 : go_assert(pmf->is_field_name("typ"));
10143 17382 : mvals->push_back(Expression::make_type_descriptor(pm->type(), bloc));
10144 :
10145 17382 : ++pmf;
10146 17382 : go_assert(pmf == mfields->end());
10147 :
10148 17382 : e = Expression::make_struct_composite_literal(elemtype, mvals,
10149 : bloc);
10150 17382 : methods->push_back(e);
10151 17382 : }
10152 : }
10153 :
10154 7859 : ivals->push_back(Expression::make_slice_composite_literal(pif->type(),
10155 : methods, bloc));
10156 :
10157 7859 : ++pif;
10158 7859 : go_assert(pif == ifields->end());
10159 :
10160 7859 : return Expression::make_struct_composite_literal(itdt, ivals, bloc);
10161 : }
10162 :
10163 : // Reflection string.
10164 :
10165 : void
10166 9193 : Interface_type::do_reflection(Gogo* gogo, std::string* ret) const
10167 : {
10168 9193 : ret->append("interface {");
10169 9193 : const Typed_identifier_list* methods = this->parse_methods_;
10170 9193 : if (methods != NULL)
10171 : {
10172 143 : ret->push_back(' ');
10173 300 : for (Typed_identifier_list::const_iterator p = methods->begin();
10174 300 : p != methods->end();
10175 157 : ++p)
10176 : {
10177 157 : if (p != methods->begin())
10178 14 : ret->append("; ");
10179 157 : if (p->name().empty())
10180 8 : this->append_reflection(p->type(), gogo, ret);
10181 : else
10182 : {
10183 149 : if (!Gogo::is_hidden_name(p->name()))
10184 120 : ret->append(p->name());
10185 29 : else if (gogo->pkgpath_from_option())
10186 24 : ret->append(p->name().substr(1));
10187 : else
10188 : {
10189 : // If no -fgo-pkgpath option, backward compatibility
10190 : // for how this used to work before -fgo-pkgpath was
10191 : // introduced.
10192 17 : std::string pkgpath = Gogo::hidden_name_pkgpath(p->name());
10193 34 : ret->append(pkgpath.substr(pkgpath.find('.') + 1));
10194 17 : ret->push_back('.');
10195 34 : ret->append(Gogo::unpack_hidden_name(p->name()));
10196 17 : }
10197 149 : std::string sub = p->type()->reflection(gogo);
10198 149 : go_assert(sub.compare(0, 4, "func") == 0);
10199 149 : sub = sub.substr(4);
10200 298 : ret->append(sub);
10201 149 : }
10202 : }
10203 143 : ret->push_back(' ');
10204 : }
10205 9193 : ret->append("}");
10206 9193 : }
10207 :
10208 : // Export.
10209 :
10210 : void
10211 11558 : Interface_type::do_export(Export* exp) const
10212 : {
10213 11558 : exp->write_c_string("interface { ");
10214 :
10215 11558 : const Typed_identifier_list* methods = this->parse_methods_;
10216 11558 : if (methods != NULL)
10217 : {
10218 51537 : for (Typed_identifier_list::const_iterator pm = methods->begin();
10219 51537 : pm != methods->end();
10220 39979 : ++pm)
10221 : {
10222 39979 : if (pm->name().empty())
10223 : {
10224 2716 : exp->write_c_string("? ");
10225 2716 : exp->write_type(pm->type());
10226 : }
10227 : else
10228 : {
10229 37263 : exp->write_string(pm->name());
10230 37263 : exp->write_c_string(" (");
10231 :
10232 37263 : const Function_type* fntype = pm->type()->function_type();
10233 :
10234 37263 : bool first = true;
10235 37263 : const Typed_identifier_list* parameters = fntype->parameters();
10236 37263 : if (parameters != NULL)
10237 : {
10238 14691 : bool is_varargs = fntype->is_varargs();
10239 33976 : for (Typed_identifier_list::const_iterator pp =
10240 14691 : parameters->begin();
10241 33976 : pp != parameters->end();
10242 19285 : ++pp)
10243 : {
10244 19285 : if (first)
10245 : first = false;
10246 : else
10247 4594 : exp->write_c_string(", ");
10248 19285 : exp->write_name(pp->name());
10249 19285 : exp->write_c_string(" ");
10250 19285 : if (!is_varargs || pp + 1 != parameters->end())
10251 19121 : exp->write_type(pp->type());
10252 : else
10253 : {
10254 164 : exp->write_c_string("...");
10255 164 : Type *pptype = pp->type();
10256 328 : exp->write_type(pptype->array_type()->element_type());
10257 : }
10258 : }
10259 : }
10260 :
10261 37263 : exp->write_c_string(")");
10262 :
10263 37263 : const Typed_identifier_list* results = fntype->results();
10264 37263 : if (results != NULL)
10265 : {
10266 31989 : exp->write_c_string(" ");
10267 31989 : if (results->size() == 1 && results->begin()->name().empty())
10268 24420 : exp->write_type(results->begin()->type());
10269 : else
10270 : {
10271 7569 : first = true;
10272 7569 : exp->write_c_string("(");
10273 23223 : for (Typed_identifier_list::const_iterator p =
10274 7569 : results->begin();
10275 23223 : p != results->end();
10276 15654 : ++p)
10277 : {
10278 15654 : if (first)
10279 : first = false;
10280 : else
10281 8085 : exp->write_c_string(", ");
10282 15654 : exp->write_name(p->name());
10283 15654 : exp->write_c_string(" ");
10284 15654 : exp->write_type(p->type());
10285 : }
10286 7569 : exp->write_c_string(")");
10287 : }
10288 : }
10289 : }
10290 :
10291 39979 : exp->write_c_string("; ");
10292 : }
10293 : }
10294 :
10295 11558 : exp->write_c_string("}");
10296 11558 : }
10297 :
10298 : // Import an interface type.
10299 :
10300 : Interface_type*
10301 65117 : Interface_type::do_import(Import* imp)
10302 : {
10303 65117 : imp->require_c_string("interface { ");
10304 :
10305 65117 : Typed_identifier_list* methods = new Typed_identifier_list;
10306 284340 : while (imp->peek_char() != '}')
10307 : {
10308 219223 : std::string name = imp->read_identifier();
10309 :
10310 219223 : if (name == "?")
10311 : {
10312 26301 : imp->require_c_string(" ");
10313 26301 : Type* t = imp->read_type();
10314 52602 : methods->push_back(Typed_identifier("", t, imp->location()));
10315 26301 : imp->require_c_string("; ");
10316 26301 : continue;
10317 26301 : }
10318 :
10319 192922 : imp->require_c_string(" (");
10320 :
10321 192922 : Typed_identifier_list* parameters;
10322 192922 : bool is_varargs = false;
10323 192922 : if (imp->peek_char() == ')')
10324 : parameters = NULL;
10325 : else
10326 : {
10327 83082 : parameters = new Typed_identifier_list;
10328 155766 : while (true)
10329 : {
10330 119424 : std::string pname = imp->read_name();
10331 119424 : imp->require_c_string(" ");
10332 :
10333 119424 : if (imp->match_c_string("..."))
10334 : {
10335 7641 : imp->advance(3);
10336 7641 : is_varargs = true;
10337 : }
10338 :
10339 119424 : Type* ptype = imp->read_type();
10340 119424 : if (is_varargs)
10341 7641 : ptype = Type::make_array_type(ptype, NULL);
10342 119424 : parameters->push_back(Typed_identifier(pname, ptype,
10343 238848 : imp->location()));
10344 119424 : if (imp->peek_char() != ',')
10345 : break;
10346 36342 : go_assert(!is_varargs);
10347 36342 : imp->require_c_string(", ");
10348 119424 : }
10349 : }
10350 192922 : imp->require_c_string(")");
10351 :
10352 192922 : Typed_identifier_list* results;
10353 192922 : if (imp->peek_char() != ' ')
10354 : results = NULL;
10355 : else
10356 : {
10357 156795 : results = new Typed_identifier_list;
10358 156795 : imp->advance(1);
10359 156795 : if (imp->peek_char() != '(')
10360 : {
10361 114233 : Type* rtype = imp->read_type();
10362 228466 : results->push_back(Typed_identifier("", rtype, imp->location()));
10363 : }
10364 : else
10365 : {
10366 42562 : imp->advance(1);
10367 135644 : while (true)
10368 : {
10369 89103 : std::string rname = imp->read_name();
10370 89103 : imp->require_c_string(" ");
10371 89103 : Type* rtype = imp->read_type();
10372 89103 : results->push_back(Typed_identifier(rname, rtype,
10373 178206 : imp->location()));
10374 89103 : if (imp->peek_char() != ',')
10375 : break;
10376 46541 : imp->require_c_string(", ");
10377 46541 : }
10378 42562 : imp->require_c_string(")");
10379 : }
10380 : }
10381 :
10382 192922 : Function_type* fntype = Type::make_function_type(NULL, parameters,
10383 : results,
10384 192922 : imp->location());
10385 192922 : if (is_varargs)
10386 7641 : fntype->set_is_varargs();
10387 385844 : methods->push_back(Typed_identifier(name, fntype, imp->location()));
10388 :
10389 192922 : imp->require_c_string("; ");
10390 219223 : }
10391 :
10392 65117 : imp->require_c_string("}");
10393 :
10394 65117 : if (methods->empty())
10395 : {
10396 0 : delete methods;
10397 0 : methods = NULL;
10398 : }
10399 :
10400 65117 : Interface_type* ret = Type::make_interface_type(methods, imp->location());
10401 65117 : ret->package_ = imp->package();
10402 65117 : return ret;
10403 : }
10404 :
10405 : // Make an interface type.
10406 :
10407 : Interface_type*
10408 72987 : Type::make_interface_type(Typed_identifier_list* methods,
10409 : Location location)
10410 : {
10411 72987 : return new Interface_type(methods, location);
10412 : }
10413 :
10414 : // Make an empty interface type.
10415 :
10416 : Interface_type*
10417 50325 : Type::make_empty_interface_type(Location location)
10418 : {
10419 50325 : Interface_type* ret = new Interface_type(NULL, location);
10420 50325 : ret->finalize_methods();
10421 50325 : return ret;
10422 : }
10423 :
10424 : // Class Method.
10425 :
10426 : // Bind a method to an object.
10427 :
10428 : Expression*
10429 259731 : Method::bind_method(Expression* expr, Location location) const
10430 : {
10431 259731 : if (this->stub_ == NULL)
10432 : {
10433 : // When there is no stub object, the binding is determined by
10434 : // the child class.
10435 219755 : return this->do_bind_method(expr, location);
10436 : }
10437 39976 : return Expression::make_bound_method(expr, this, this->stub_, location);
10438 : }
10439 :
10440 : // Return the named object associated with a method. This may only be
10441 : // called after methods are finalized.
10442 :
10443 : Named_object*
10444 3124438 : Method::named_object() const
10445 : {
10446 3124438 : if (this->stub_ != NULL)
10447 : return this->stub_;
10448 2507023 : return this->do_named_object();
10449 : }
10450 :
10451 : // Class Named_method.
10452 :
10453 : // The type of the method.
10454 :
10455 : Function_type*
10456 9283674 : Named_method::do_type() const
10457 : {
10458 9283674 : if (this->named_object_->is_function())
10459 390936 : return this->named_object_->func_value()->type();
10460 8892738 : else if (this->named_object_->is_function_declaration())
10461 8892738 : return this->named_object_->func_declaration_value()->type();
10462 : else
10463 0 : go_unreachable();
10464 : }
10465 :
10466 : // Return the location of the method receiver.
10467 :
10468 : Location
10469 301121 : Named_method::do_receiver_location() const
10470 : {
10471 301121 : return this->do_type()->receiver()->location();
10472 : }
10473 :
10474 : // Bind a method to an object.
10475 :
10476 : Expression*
10477 218594 : Named_method::do_bind_method(Expression* expr, Location location) const
10478 : {
10479 218594 : Named_object* no = this->named_object_;
10480 218594 : Bound_method_expression* bme = Expression::make_bound_method(expr, this,
10481 : no, location);
10482 : // If this is not a local method, and it does not use a stub, then
10483 : // the real method expects a different type. We need to cast the
10484 : // first argument.
10485 218594 : if (this->depth() > 0 && !this->needs_stub_method())
10486 : {
10487 0 : Function_type* ftype = this->do_type();
10488 0 : go_assert(ftype->is_method());
10489 0 : Type* frtype = ftype->receiver()->type();
10490 0 : bme->set_first_argument_type(frtype);
10491 : }
10492 218594 : return bme;
10493 : }
10494 :
10495 : // Return whether this method should not participate in interfaces.
10496 :
10497 : bool
10498 1492122 : Named_method::do_nointerface() const
10499 : {
10500 1492122 : Named_object* no = this->named_object_;
10501 1492122 : if (no->is_function())
10502 451411 : return no->func_value()->nointerface();
10503 1040711 : else if (no->is_function_declaration())
10504 1040711 : return no->func_declaration_value()->nointerface();
10505 : else
10506 0 : go_unreachable();
10507 : }
10508 :
10509 : // Class Interface_method.
10510 :
10511 : // Bind a method to an object.
10512 :
10513 : Expression*
10514 1161 : Interface_method::do_bind_method(Expression* expr,
10515 : Location location) const
10516 : {
10517 1161 : return Expression::make_interface_field_reference(expr, this->name_,
10518 1161 : location);
10519 : }
10520 :
10521 : // Class Methods.
10522 :
10523 : // Insert a new method. Return true if it was inserted, false
10524 : // otherwise.
10525 :
10526 : bool
10527 2291740 : Methods::insert(const std::string& name, Method* m)
10528 : {
10529 2291740 : std::pair<Method_map::iterator, bool> ins =
10530 4583480 : this->methods_.insert(std::make_pair(name, m));
10531 2291740 : if (ins.second)
10532 : return true;
10533 : else
10534 : {
10535 17690 : Method* old_method = ins.first->second;
10536 17690 : if (m->depth() < old_method->depth())
10537 : {
10538 2 : delete old_method;
10539 2 : ins.first->second = m;
10540 2 : return true;
10541 : }
10542 : else
10543 : {
10544 17688 : if (m->depth() == old_method->depth())
10545 1043 : old_method->set_is_ambiguous();
10546 17688 : return false;
10547 : }
10548 : }
10549 : }
10550 :
10551 : // Return the number of unambiguous methods.
10552 :
10553 : size_t
10554 56522 : Methods::count() const
10555 : {
10556 56522 : size_t ret = 0;
10557 525486 : for (Method_map::const_iterator p = this->methods_.begin();
10558 525486 : p != this->methods_.end();
10559 468964 : ++p)
10560 468964 : if (!p->second->is_ambiguous())
10561 468844 : ++ret;
10562 56522 : return ret;
10563 : }
10564 :
10565 : // Class Named_type.
10566 :
10567 : // Return the name of the type.
10568 :
10569 : const std::string&
10570 3492105 : Named_type::name() const
10571 : {
10572 3492105 : return this->named_object_->name();
10573 : }
10574 :
10575 : // Return the name of the type to use in an error message.
10576 :
10577 : void
10578 211409 : Named_type::do_message_name(std::string* ret) const
10579 : {
10580 422818 : ret->append(this->named_object_->message_name());
10581 211409 : }
10582 :
10583 : // Return the base type for this type. We have to be careful about
10584 : // circular type definitions, which are invalid but may be seen here.
10585 :
10586 : Type*
10587 103884666 : Named_type::named_base()
10588 : {
10589 103884666 : if (this->seen_)
10590 48 : return this;
10591 103884618 : this->seen_ = true;
10592 103884618 : Type* ret = this->type_->base();
10593 103884618 : this->seen_ = false;
10594 103884618 : return ret;
10595 : }
10596 :
10597 : const Type*
10598 201639547 : Named_type::named_base() const
10599 : {
10600 201639547 : if (this->seen_)
10601 0 : return this;
10602 201639547 : this->seen_ = true;
10603 201639547 : const Type* ret = this->type_->base();
10604 201639547 : this->seen_ = false;
10605 201639547 : return ret;
10606 : }
10607 :
10608 : // Return whether this is an error type. We have to be careful about
10609 : // circular type definitions, which are invalid but may be seen here.
10610 :
10611 : bool
10612 882472144 : Named_type::is_named_error_type() const
10613 : {
10614 882472144 : if (this->seen_)
10615 : return false;
10616 882472122 : this->seen_ = true;
10617 882472122 : bool ret = this->type_->is_error_type();
10618 882472122 : this->seen_ = false;
10619 882472122 : return ret;
10620 : }
10621 :
10622 : // Whether this type is comparable. We have to be careful about
10623 : // circular type definitions.
10624 :
10625 : bool
10626 6203209 : Named_type::named_type_is_comparable(std::string* reason) const
10627 : {
10628 6203209 : if (this->seen_)
10629 : return false;
10630 6203209 : this->seen_ = true;
10631 12406418 : bool ret = Type::are_compatible_for_comparison(true, this->type_,
10632 6203209 : this->type_, reason);
10633 6203209 : this->seen_ = false;
10634 6203209 : return ret;
10635 : }
10636 :
10637 : // Add a method to this type.
10638 :
10639 : Named_object*
10640 51240 : Named_type::add_method(const std::string& name, Function* function)
10641 : {
10642 51240 : go_assert(!this->is_alias_);
10643 51240 : if (this->local_methods_ == NULL)
10644 11146 : this->local_methods_ = new Bindings(NULL);
10645 51240 : return this->local_methods_->add_function(name,
10646 51240 : this->named_object_->package(),
10647 51240 : function);
10648 : }
10649 :
10650 : // Add a method declaration to this type.
10651 :
10652 : Named_object*
10653 1784527 : Named_type::add_method_declaration(const std::string& name, Package* package,
10654 : Function_type* type,
10655 : Location location)
10656 : {
10657 1784527 : go_assert(!this->is_alias_);
10658 1784527 : if (this->local_methods_ == NULL)
10659 293661 : this->local_methods_ = new Bindings(NULL);
10660 1784527 : return this->local_methods_->add_function_declaration(name, package, type,
10661 1784527 : location);
10662 : }
10663 :
10664 : // Add an existing method to this type.
10665 :
10666 : void
10667 2536 : Named_type::add_existing_method(Named_object* no)
10668 : {
10669 2536 : go_assert(!this->is_alias_);
10670 2536 : if (this->local_methods_ == NULL)
10671 517 : this->local_methods_ = new Bindings(NULL);
10672 2536 : this->local_methods_->add_named_object(no);
10673 2536 : }
10674 :
10675 : // Look for a local method NAME, and returns its named object, or NULL
10676 : // if not there.
10677 :
10678 : Named_object*
10679 1229803 : Named_type::find_local_method(const std::string& name) const
10680 : {
10681 1229803 : if (this->is_error_)
10682 : return NULL;
10683 1229803 : if (this->is_alias_)
10684 : {
10685 0 : Named_type* nt = this->type_->named_type();
10686 0 : if (nt != NULL)
10687 : {
10688 0 : if (this->seen_alias_)
10689 : return NULL;
10690 0 : this->seen_alias_ = true;
10691 0 : Named_object* ret = nt->find_local_method(name);
10692 0 : this->seen_alias_ = false;
10693 0 : return ret;
10694 : }
10695 : return NULL;
10696 : }
10697 1229803 : if (this->local_methods_ == NULL)
10698 : return NULL;
10699 627315 : return this->local_methods_->lookup(name);
10700 : }
10701 :
10702 : // Return the list of local methods.
10703 :
10704 : const Bindings*
10705 5400745 : Named_type::local_methods() const
10706 : {
10707 5400745 : if (this->is_error_)
10708 : return NULL;
10709 5400545 : if (this->is_alias_)
10710 : {
10711 6734 : Named_type* nt = this->type_->named_type();
10712 6734 : if (nt != NULL)
10713 : {
10714 5922 : if (this->seen_alias_)
10715 : return NULL;
10716 5922 : this->seen_alias_ = true;
10717 5922 : const Bindings* ret = nt->local_methods();
10718 5922 : this->seen_alias_ = false;
10719 5922 : return ret;
10720 : }
10721 : return NULL;
10722 : }
10723 5393811 : return this->local_methods_;
10724 : }
10725 :
10726 : // Return whether NAME is an unexported field or method, for better
10727 : // error reporting.
10728 :
10729 : bool
10730 22 : Named_type::is_unexported_local_method(Gogo* gogo,
10731 : const std::string& name) const
10732 : {
10733 22 : if (this->is_error_)
10734 : return false;
10735 22 : if (this->is_alias_)
10736 : {
10737 0 : Named_type* nt = this->type_->named_type();
10738 0 : if (nt != NULL)
10739 : {
10740 0 : if (this->seen_alias_)
10741 : return false;
10742 0 : this->seen_alias_ = true;
10743 0 : bool ret = nt->is_unexported_local_method(gogo, name);
10744 0 : this->seen_alias_ = false;
10745 0 : return ret;
10746 : }
10747 : return false;
10748 : }
10749 22 : Bindings* methods = this->local_methods_;
10750 22 : if (methods != NULL)
10751 : {
10752 46 : for (Bindings::const_declarations_iterator p =
10753 7 : methods->begin_declarations();
10754 46 : p != methods->end_declarations();
10755 39 : ++p)
10756 : {
10757 80 : if (Gogo::is_hidden_name(p->first)
10758 66 : && name == Gogo::unpack_hidden_name(p->first)
10759 68 : && gogo->pack_hidden_name(name, false) != p->first)
10760 22 : return true;
10761 : }
10762 : }
10763 : return false;
10764 : }
10765 :
10766 : // Build the complete list of methods for this type, which means
10767 : // recursively including all methods for anonymous fields. Create all
10768 : // stub methods.
10769 :
10770 : void
10771 2192698 : Named_type::finalize_methods(Gogo* gogo)
10772 : {
10773 2192698 : if (this->is_alias_)
10774 : return;
10775 2192698 : if (this->all_methods_ != NULL)
10776 : return;
10777 :
10778 1449562 : if (this->local_methods_ != NULL
10779 1449562 : && (this->points_to() != NULL || this->interface_type() != NULL))
10780 : {
10781 5 : const Bindings* lm = this->local_methods_;
10782 10 : for (Bindings::const_declarations_iterator p = lm->begin_declarations();
10783 10 : p != lm->end_declarations();
10784 5 : ++p)
10785 5 : go_error_at(p->second->location(),
10786 : "invalid pointer or interface receiver type");
10787 10 : delete this->local_methods_;
10788 5 : this->local_methods_ = NULL;
10789 5 : return;
10790 : }
10791 :
10792 : // Remove any aliases in the local method receiver types.
10793 1449557 : Bindings* methods = this->local_methods_;
10794 1449557 : if (methods != NULL)
10795 : {
10796 2143009 : for (Bindings::const_declarations_iterator p =
10797 305319 : methods->begin_declarations();
10798 2143009 : p != methods->end_declarations();
10799 1837690 : ++p)
10800 : {
10801 1837690 : Named_object* no = p->second;
10802 1837690 : Function_type* fntype;
10803 1837690 : if (no->is_function())
10804 53163 : fntype = no->func_value()->type();
10805 1784527 : else if (no->is_function_declaration())
10806 1784527 : fntype = no->func_declaration_value()->type();
10807 : else
10808 : {
10809 0 : go_assert(saw_errors());
10810 0 : continue;
10811 : }
10812 :
10813 1837690 : Type* rtype = fntype->receiver()->type();
10814 1837690 : bool is_pointer = false;
10815 1837690 : Type* pt = rtype->points_to();
10816 1837690 : if (pt != NULL)
10817 : {
10818 1395035 : rtype = pt;
10819 1395035 : is_pointer = true;
10820 : }
10821 1837690 : if (rtype->named_type() != this)
10822 : {
10823 2 : if (rtype->unalias() != this)
10824 : {
10825 0 : go_assert(saw_errors());
10826 0 : continue;
10827 : }
10828 :
10829 2 : rtype = this;
10830 2 : if (is_pointer)
10831 0 : rtype = Type::make_pointer_type(rtype);
10832 :
10833 2 : if (no->is_function())
10834 2 : no->func_value()->set_receiver_type(rtype);
10835 0 : else if (no->is_function_declaration())
10836 0 : no->func_declaration_value()->set_receiver_type(rtype);
10837 : else
10838 0 : go_unreachable();
10839 : }
10840 : }
10841 : }
10842 :
10843 1449557 : Type::finalize_methods(gogo, this, this->location_, &this->all_methods_);
10844 : }
10845 :
10846 : // Return whether this type has any methods.
10847 :
10848 : bool
10849 17176995 : Named_type::has_any_methods() const
10850 : {
10851 17176995 : if (this->is_error_)
10852 : return false;
10853 17176995 : if (this->is_alias_)
10854 : {
10855 0 : if (this->type_->named_type() != NULL)
10856 : {
10857 0 : if (this->seen_alias_)
10858 : return false;
10859 0 : this->seen_alias_ = true;
10860 0 : bool ret = this->type_->named_type()->has_any_methods();
10861 0 : this->seen_alias_ = false;
10862 0 : return ret;
10863 : }
10864 0 : if (this->type_->struct_type() != NULL)
10865 0 : return this->type_->struct_type()->has_any_methods();
10866 : return false;
10867 : }
10868 17176995 : return this->all_methods_ != NULL;
10869 : }
10870 :
10871 : // Return the methods for this type.
10872 :
10873 : const Methods*
10874 1165394 : Named_type::methods() const
10875 : {
10876 1165394 : if (this->is_error_)
10877 : return NULL;
10878 1165394 : if (this->is_alias_)
10879 : {
10880 198 : if (this->type_->named_type() != NULL)
10881 : {
10882 145 : if (this->seen_alias_)
10883 : return NULL;
10884 145 : this->seen_alias_ = true;
10885 145 : const Methods* ret = this->type_->named_type()->methods();
10886 145 : this->seen_alias_ = false;
10887 145 : return ret;
10888 : }
10889 100 : if (this->type_->struct_type() != NULL)
10890 12 : return this->type_->struct_type()->methods();
10891 : return NULL;
10892 : }
10893 1165196 : return this->all_methods_;
10894 : }
10895 :
10896 : // Return the method NAME, or NULL if there isn't one or if it is
10897 : // ambiguous. Set *IS_AMBIGUOUS if the method exists but is
10898 : // ambiguous.
10899 :
10900 : Method*
10901 4029455 : Named_type::method_function(const std::string& name, bool* is_ambiguous) const
10902 : {
10903 4029455 : if (this->is_error_)
10904 : return NULL;
10905 4029455 : if (this->is_alias_)
10906 : {
10907 191 : if (is_ambiguous != NULL)
10908 1 : *is_ambiguous = false;
10909 191 : if (this->type_->named_type() != NULL)
10910 : {
10911 189 : if (this->seen_alias_)
10912 : return NULL;
10913 189 : this->seen_alias_ = true;
10914 189 : Named_type* nt = this->type_->named_type();
10915 189 : Method* ret = nt->method_function(name, is_ambiguous);
10916 189 : this->seen_alias_ = false;
10917 189 : return ret;
10918 : }
10919 2 : if (this->type_->struct_type() != NULL)
10920 4 : return this->type_->struct_type()->method_function(name, is_ambiguous);
10921 : return NULL;
10922 : }
10923 4029264 : return Type::method_function(this->all_methods_, name, is_ambiguous);
10924 : }
10925 :
10926 : // Return a pointer to the interface method table for this type for
10927 : // the interface INTERFACE. IS_POINTER is true if this is for a
10928 : // pointer to THIS.
10929 :
10930 : Expression*
10931 182943 : Named_type::interface_method_table(Interface_type* interface, bool is_pointer)
10932 : {
10933 182943 : if (this->is_error_)
10934 0 : return Expression::make_error(this->location_);
10935 182943 : if (this->is_alias_)
10936 : {
10937 566 : Type* t = this->type_;
10938 566 : if (!is_pointer && t->points_to() != NULL)
10939 : {
10940 1 : t = t->points_to();
10941 1 : is_pointer = true;
10942 : }
10943 566 : if (t->named_type() != NULL)
10944 : {
10945 566 : if (this->seen_alias_)
10946 0 : return Expression::make_error(this->location_);
10947 566 : this->seen_alias_ = true;
10948 566 : Named_type* nt = t->named_type();
10949 566 : Expression* ret = nt->interface_method_table(interface, is_pointer);
10950 566 : this->seen_alias_ = false;
10951 566 : return ret;
10952 : }
10953 0 : if (t->struct_type() != NULL)
10954 0 : return t->struct_type()->interface_method_table(interface, is_pointer);
10955 0 : go_unreachable();
10956 : }
10957 182377 : return Type::interface_method_table(this, interface, is_pointer,
10958 : &this->interface_method_tables_,
10959 182377 : &this->pointer_interface_method_tables_);
10960 : }
10961 :
10962 : // Look for a use of a complete type within another type. This is
10963 : // used to check that we don't try to use a type within itself.
10964 :
10965 784525 : class Find_type_use : public Traverse
10966 : {
10967 : public:
10968 784525 : Find_type_use(Named_type* find_type)
10969 784525 : : Traverse(traverse_types),
10970 784525 : find_type_(find_type), found_(false)
10971 : { }
10972 :
10973 : // Whether we found the type.
10974 : bool
10975 784525 : found() const
10976 784525 : { return this->found_; }
10977 :
10978 : protected:
10979 : int
10980 : type(Type*);
10981 :
10982 : private:
10983 : // The type we are looking for.
10984 : Named_type* find_type_;
10985 : // Whether we found the type.
10986 : bool found_;
10987 : };
10988 :
10989 : // Check for FIND_TYPE in TYPE.
10990 :
10991 : int
10992 4865728 : Find_type_use::type(Type* type)
10993 : {
10994 4865728 : if (type->named_type() != NULL && this->find_type_ == type->named_type())
10995 : {
10996 13 : this->found_ = true;
10997 13 : return TRAVERSE_EXIT;
10998 : }
10999 :
11000 : // It's OK if we see a reference to the type in any type which is
11001 : // essentially a pointer: a pointer, a slice, a function, a map, or
11002 : // a channel.
11003 4865715 : if (type->points_to() != NULL
11004 4205261 : || type->is_slice_type()
11005 3951502 : || type->function_type() != NULL
11006 4883061 : || type->map_type() != NULL
11007 5170906 : || type->channel_type() != NULL)
11008 965645 : return TRAVERSE_SKIP_COMPONENTS;
11009 :
11010 : // For an interface, a reference to the type in a method type should
11011 : // be ignored, but we have to consider direct inheritance. When
11012 : // this is called, there may be cases of direct inheritance
11013 : // represented as a method with no name.
11014 3900070 : if (type->interface_type() != NULL)
11015 : {
11016 321794 : const Typed_identifier_list* methods = type->interface_type()->methods();
11017 160897 : if (methods != NULL)
11018 : {
11019 663688 : for (Typed_identifier_list::const_iterator p = methods->begin();
11020 663688 : p != methods->end();
11021 523558 : ++p)
11022 : {
11023 523558 : if (p->name().empty())
11024 : {
11025 0 : if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
11026 16 : return TRAVERSE_EXIT;
11027 : }
11028 : }
11029 : }
11030 160897 : return TRAVERSE_SKIP_COMPONENTS;
11031 : }
11032 :
11033 : // Otherwise, FIND_TYPE_ depends on TYPE, in the sense that we need
11034 : // to convert TYPE to the backend representation before we convert
11035 : // FIND_TYPE_.
11036 3739173 : if (type->named_type() != NULL)
11037 : {
11038 1655189 : switch (type->base()->classification())
11039 : {
11040 : case Type::TYPE_ERROR:
11041 : case Type::TYPE_BOOLEAN:
11042 : case Type::TYPE_INTEGER:
11043 : case Type::TYPE_FLOAT:
11044 : case Type::TYPE_COMPLEX:
11045 : case Type::TYPE_STRING:
11046 : case Type::TYPE_NIL:
11047 : break;
11048 :
11049 295636 : case Type::TYPE_ARRAY:
11050 295636 : case Type::TYPE_STRUCT:
11051 295636 : this->find_type_->add_dependency(type->named_type());
11052 295636 : break;
11053 :
11054 3 : case Type::TYPE_NAMED:
11055 3 : if (type->named_type() == type->base()->named_type())
11056 : {
11057 3 : this->found_ = true;
11058 3 : return TRAVERSE_EXIT;
11059 : }
11060 : else
11061 0 : go_assert(saw_errors());
11062 : break;
11063 :
11064 0 : case Type::TYPE_FORWARD:
11065 0 : go_assert(saw_errors());
11066 : break;
11067 :
11068 0 : case Type::TYPE_VOID:
11069 0 : case Type::TYPE_SINK:
11070 0 : case Type::TYPE_FUNCTION:
11071 0 : case Type::TYPE_POINTER:
11072 0 : case Type::TYPE_CALL_MULTIPLE_RESULT:
11073 0 : case Type::TYPE_MAP:
11074 0 : case Type::TYPE_CHANNEL:
11075 0 : case Type::TYPE_INTERFACE:
11076 0 : default:
11077 0 : go_unreachable();
11078 : }
11079 : }
11080 :
11081 : return TRAVERSE_CONTINUE;
11082 : }
11083 :
11084 : // Look for a circular reference of an alias.
11085 :
11086 27424 : class Find_alias : public Traverse
11087 : {
11088 : public:
11089 27424 : Find_alias(Named_type* find_type)
11090 27424 : : Traverse(traverse_types),
11091 27424 : find_type_(find_type), found_(false)
11092 : { }
11093 :
11094 : // Whether we found the type.
11095 : bool
11096 27424 : found() const
11097 27424 : { return this->found_; }
11098 :
11099 : protected:
11100 : int
11101 : type(Type*);
11102 :
11103 : private:
11104 : // The type we are looking for.
11105 : Named_type* find_type_;
11106 : // Whether we found the type.
11107 : bool found_;
11108 : };
11109 :
11110 : int
11111 40367 : Find_alias::type(Type* type)
11112 : {
11113 40367 : Named_type* nt = type->named_type();
11114 40367 : if (nt != NULL)
11115 : {
11116 29972 : if (nt == this->find_type_)
11117 : {
11118 0 : this->found_ = true;
11119 0 : return TRAVERSE_EXIT;
11120 : }
11121 :
11122 : // We started from `type T1 = T2`, where T1 is find_type_ and T2
11123 : // is, perhaps indirectly, the parameter TYPE. If TYPE is not
11124 : // an alias itself, it's OK if whatever T2 is defined as refers
11125 : // to T1.
11126 29972 : if (!nt->is_alias())
11127 : return TRAVERSE_SKIP_COMPONENTS;
11128 : }
11129 :
11130 : // Check if there are recursive inherited interface aliases.
11131 13309 : Interface_type* ift = type->interface_type();
11132 7511 : if (ift != NULL)
11133 : {
11134 7511 : const Typed_identifier_list* methods = ift->local_methods();
11135 7511 : if (methods == NULL)
11136 : return TRAVERSE_CONTINUE;
11137 24 : for (Typed_identifier_list::const_iterator p = methods->begin();
11138 24 : p != methods->end();
11139 17 : ++p)
11140 17 : if (p->name().empty() && p->type()->named_type() == this->find_type_)
11141 : {
11142 0 : this->found_ = true;
11143 0 : return TRAVERSE_EXIT;
11144 : }
11145 : }
11146 :
11147 : return TRAVERSE_CONTINUE;
11148 : }
11149 :
11150 : // Verify that a named type does not refer to itself.
11151 :
11152 : bool
11153 2679736 : Named_type::do_verify(Gogo*)
11154 : {
11155 2679736 : if (this->is_verified_)
11156 : return true;
11157 784552 : this->is_verified_ = true;
11158 :
11159 784552 : if (this->is_error_)
11160 : return false;
11161 :
11162 784525 : if (this->is_alias_)
11163 : {
11164 27424 : Find_alias find(this);
11165 27424 : Type::traverse(this->type_, &find);
11166 27424 : if (find.found())
11167 : {
11168 0 : go_error_at(this->location_, "invalid recursive alias %qs",
11169 0 : this->message_name().c_str());
11170 0 : this->is_error_ = true;
11171 0 : return false;
11172 : }
11173 27424 : }
11174 :
11175 784525 : Find_type_use find(this);
11176 784525 : Type::traverse(this->type_, &find);
11177 784525 : if (find.found())
11178 : {
11179 16 : go_error_at(this->location_, "invalid recursive type %qs",
11180 16 : this->message_name().c_str());
11181 16 : this->is_error_ = true;
11182 16 : return false;
11183 : }
11184 :
11185 : // Check whether any of the local methods overloads an existing
11186 : // struct field or interface method. We don't need to check the
11187 : // list of methods against itself: that is handled by the Bindings
11188 : // code.
11189 784509 : if (this->local_methods_ != NULL)
11190 : {
11191 305318 : Struct_type* st = this->type_->struct_type();
11192 234388 : if (st != NULL)
11193 : {
11194 1589298 : for (Bindings::const_declarations_iterator p =
11195 234388 : this->local_methods_->begin_declarations();
11196 1823686 : p != this->local_methods_->end_declarations();
11197 1589298 : ++p)
11198 : {
11199 1589298 : const std::string& name(p->first);
11200 1589298 : if (st != NULL && st->find_local_field(name, NULL) != NULL)
11201 : {
11202 3 : go_error_at(p->second->location(),
11203 : "method %qs redeclares struct field name",
11204 6 : Gogo::message_name(name).c_str());
11205 : }
11206 : }
11207 : }
11208 : }
11209 :
11210 : return true;
11211 784525 : }
11212 :
11213 : // Return whether this type is or contains a pointer.
11214 :
11215 : bool
11216 6537439 : Named_type::do_has_pointer() const
11217 : {
11218 : // A type that is not in the heap has no pointers that we care about.
11219 6537439 : if (!this->in_heap_)
11220 : return false;
11221 :
11222 6536701 : if (this->seen_)
11223 : return false;
11224 6536701 : this->seen_ = true;
11225 6536701 : bool ret = this->type_->has_pointer();
11226 6536701 : this->seen_ = false;
11227 6536701 : return ret;
11228 : }
11229 :
11230 : // Return whether comparisons for this type can use the identity
11231 : // function.
11232 :
11233 : bool
11234 4381022 : Named_type::do_compare_is_identity(Gogo* gogo)
11235 : {
11236 : // We don't use this->seen_ here because compare_is_identity may
11237 : // call base() later, and that will mess up if seen_ is set here.
11238 4381022 : if (this->seen_in_compare_is_identity_)
11239 : return false;
11240 4381022 : this->seen_in_compare_is_identity_ = true;
11241 4381022 : bool ret = this->type_->compare_is_identity(gogo);
11242 4381022 : this->seen_in_compare_is_identity_ = false;
11243 4381022 : return ret;
11244 : }
11245 :
11246 : // Return whether this type is reflexive--whether it is always equal
11247 : // to itself.
11248 :
11249 : bool
11250 4557 : Named_type::do_is_reflexive()
11251 : {
11252 4557 : if (this->seen_in_compare_is_identity_)
11253 : return false;
11254 4557 : this->seen_in_compare_is_identity_ = true;
11255 4557 : bool ret = this->type_->is_reflexive();
11256 4557 : this->seen_in_compare_is_identity_ = false;
11257 4557 : return ret;
11258 : }
11259 :
11260 : // Return whether this type needs a key update when used as a map key.
11261 :
11262 : bool
11263 4266 : Named_type::do_needs_key_update()
11264 : {
11265 4266 : if (this->seen_in_compare_is_identity_)
11266 : return true;
11267 4266 : this->seen_in_compare_is_identity_ = true;
11268 4266 : bool ret = this->type_->needs_key_update();
11269 4266 : this->seen_in_compare_is_identity_ = false;
11270 4266 : return ret;
11271 : }
11272 :
11273 : // Return whether this type is permitted in the heap.
11274 : bool
11275 31482735 : Named_type::do_in_heap() const
11276 : {
11277 31482735 : if (!this->in_heap_)
11278 : return false;
11279 31414494 : if (this->seen_)
11280 : return true;
11281 31406936 : this->seen_ = true;
11282 31406936 : bool ret = this->type_->in_heap();
11283 31406936 : this->seen_ = false;
11284 31406936 : return ret;
11285 : }
11286 :
11287 : // Return a hash code. This is used for method lookup. We simply
11288 : // hash on the name itself.
11289 :
11290 : unsigned int
11291 10616389 : Named_type::do_hash_for_method(Gogo* gogo, int) const
11292 : {
11293 10616389 : if (this->is_error_)
11294 : return 0;
11295 :
11296 : // Aliases are handled in Type::hash_for_method.
11297 10616386 : go_assert(!this->is_alias_);
11298 :
11299 10616386 : const std::string& name(this->named_object()->name());
11300 10616386 : unsigned int ret = Gogo::hash_string(name, 0);
11301 :
11302 : // GOGO will be NULL here when called from Type_hash_identical.
11303 : // That is OK because that is only used for internal hash tables
11304 : // where we are going to be comparing named types for equality. In
11305 : // other cases, which are cases where the runtime is going to
11306 : // compare hash codes to see if the types are the same, we need to
11307 : // include the pkgpath in the hash.
11308 10975271 : if (gogo != NULL && !Gogo::is_hidden_name(name) && !this->is_builtin())
11309 : {
11310 166094 : const Package* package = this->named_object()->package();
11311 166094 : if (package == NULL)
11312 90011 : ret = Gogo::hash_string(gogo->pkgpath(), ret);
11313 : else
11314 76083 : ret = Gogo::hash_string(package->pkgpath(), ret);
11315 : }
11316 :
11317 : return ret;
11318 : }
11319 :
11320 : // Convert a named type to the backend representation. In order to
11321 : // get dependencies right, we fill in a dummy structure for this type,
11322 : // then convert all the dependencies, then complete this type. When
11323 : // this function is complete, the size of the type is known.
11324 :
11325 : void
11326 13683329 : Named_type::convert(Gogo* gogo)
11327 : {
11328 13683329 : if (this->is_error_ || this->is_converted_)
11329 : return;
11330 :
11331 784536 : this->create_placeholder(gogo);
11332 :
11333 : // If we are called to turn unsafe.Sizeof into a constant, we may
11334 : // not have verified the type yet. We have to make sure it is
11335 : // verified, since that sets the list of dependencies.
11336 784536 : this->verify(gogo);
11337 :
11338 : // Convert all the dependencies. If they refer indirectly back to
11339 : // this type, they will pick up the intermediate representation we just
11340 : // created.
11341 1080159 : for (std::vector<Named_type*>::const_iterator p = this->dependencies_.begin();
11342 1080159 : p != this->dependencies_.end();
11343 295623 : ++p)
11344 295623 : (*p)->convert(gogo);
11345 :
11346 : // Complete this type.
11347 784536 : Btype* bt = this->named_btype_;
11348 784536 : Type* base = this->type_->base();
11349 784536 : switch (base->classification())
11350 : {
11351 : case TYPE_VOID:
11352 : case TYPE_BOOLEAN:
11353 : case TYPE_INTEGER:
11354 : case TYPE_FLOAT:
11355 : case TYPE_COMPLEX:
11356 : case TYPE_STRING:
11357 : case TYPE_NIL:
11358 : break;
11359 :
11360 : case TYPE_MAP:
11361 : case TYPE_CHANNEL:
11362 : break;
11363 :
11364 : case TYPE_FUNCTION:
11365 : case TYPE_POINTER:
11366 : // The size of these types is already correct. We don't worry
11367 : // about filling them in until later, when we also track
11368 : // circular references.
11369 : break;
11370 :
11371 472272 : case TYPE_STRUCT:
11372 472272 : {
11373 472272 : std::vector<Backend::Btyped_identifier> bfields;
11374 944544 : get_backend_struct_fields(gogo, base->struct_type(), true, &bfields);
11375 472272 : if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
11376 5 : bt = gogo->backend()->error_type();
11377 472272 : }
11378 472272 : break;
11379 :
11380 18434 : case TYPE_ARRAY:
11381 : // Slice types were completed in create_placeholder.
11382 18434 : if (!base->is_slice_type())
11383 : {
11384 24792 : Btype* bet = base->array_type()->get_backend_element(gogo, true);
11385 24792 : Bexpression* blen = base->array_type()->get_backend_length(gogo);
11386 12396 : if (!gogo->backend()->set_placeholder_array_type(bt, bet, blen))
11387 0 : bt = gogo->backend()->error_type();
11388 : }
11389 : break;
11390 :
11391 : case TYPE_INTERFACE:
11392 : // Interface types were completed in create_placeholder.
11393 : break;
11394 :
11395 : case TYPE_ERROR:
11396 : return;
11397 :
11398 0 : default:
11399 0 : case TYPE_SINK:
11400 0 : case TYPE_CALL_MULTIPLE_RESULT:
11401 0 : case TYPE_NAMED:
11402 0 : case TYPE_FORWARD:
11403 0 : go_unreachable();
11404 : }
11405 :
11406 784500 : this->named_btype_ = bt;
11407 784500 : this->is_converted_ = true;
11408 784500 : this->is_placeholder_ = false;
11409 : }
11410 :
11411 : // Create the placeholder for a named type. This is the first step in
11412 : // converting to the backend representation.
11413 :
11414 : void
11415 820335 : Named_type::create_placeholder(Gogo* gogo)
11416 : {
11417 820335 : if (this->is_error_)
11418 0 : this->named_btype_ = gogo->backend()->error_type();
11419 :
11420 820335 : if (this->named_btype_ != NULL)
11421 : return;
11422 :
11423 : // Create the structure for this type. Note that because we call
11424 : // base() here, we don't attempt to represent a named type defined
11425 : // as another named type. Instead both named types will point to
11426 : // different base representations.
11427 784536 : Type* base = this->type_->base();
11428 784536 : Btype* bt;
11429 784536 : bool set_name = true;
11430 784536 : switch (base->classification())
11431 : {
11432 36 : case TYPE_ERROR:
11433 36 : this->is_error_ = true;
11434 36 : this->named_btype_ = gogo->backend()->error_type();
11435 36 : return;
11436 :
11437 202747 : case TYPE_VOID:
11438 202747 : case TYPE_BOOLEAN:
11439 202747 : case TYPE_INTEGER:
11440 202747 : case TYPE_FLOAT:
11441 202747 : case TYPE_COMPLEX:
11442 202747 : case TYPE_STRING:
11443 202747 : case TYPE_NIL:
11444 : // These are simple basic types, we can just create them
11445 : // directly.
11446 202747 : bt = Type::get_named_base_btype(gogo, base);
11447 202747 : break;
11448 :
11449 1710 : case TYPE_MAP:
11450 1710 : case TYPE_CHANNEL:
11451 : // All maps and channels have the same backend representation.
11452 1710 : bt = Type::get_named_base_btype(gogo, base);
11453 1710 : break;
11454 :
11455 9524 : case TYPE_FUNCTION:
11456 9524 : case TYPE_POINTER:
11457 9524 : {
11458 9524 : bool for_function = base->classification() == TYPE_FUNCTION;
11459 9524 : bt = gogo->backend()->placeholder_pointer_type(this->name(),
11460 : this->location_,
11461 : for_function);
11462 9524 : set_name = false;
11463 : }
11464 9524 : break;
11465 :
11466 472272 : case TYPE_STRUCT:
11467 472272 : bt = gogo->backend()->placeholder_struct_type(this->name(),
11468 : this->location_);
11469 472272 : this->is_placeholder_ = true;
11470 472272 : set_name = false;
11471 472272 : break;
11472 :
11473 18434 : case TYPE_ARRAY:
11474 18434 : if (base->is_slice_type())
11475 6038 : bt = gogo->backend()->placeholder_struct_type(this->name(),
11476 : this->location_);
11477 : else
11478 : {
11479 12396 : bt = gogo->backend()->placeholder_array_type(this->name(),
11480 : this->location_);
11481 12396 : this->is_placeholder_ = true;
11482 : }
11483 : set_name = false;
11484 : break;
11485 :
11486 79813 : case TYPE_INTERFACE:
11487 159626 : if (base->interface_type()->is_empty())
11488 5268 : bt = Interface_type::get_backend_empty_interface_type(gogo);
11489 : else
11490 : {
11491 74545 : bt = gogo->backend()->placeholder_struct_type(this->name(),
11492 : this->location_);
11493 74545 : set_name = false;
11494 : }
11495 : break;
11496 :
11497 0 : default:
11498 0 : case TYPE_SINK:
11499 0 : case TYPE_CALL_MULTIPLE_RESULT:
11500 0 : case TYPE_NAMED:
11501 0 : case TYPE_FORWARD:
11502 0 : go_unreachable();
11503 : }
11504 :
11505 766066 : if (set_name)
11506 209725 : bt = gogo->backend()->named_type(this->name(), bt, this->location_);
11507 :
11508 784500 : this->named_btype_ = bt;
11509 :
11510 784500 : if (base->is_slice_type())
11511 : {
11512 : // We do not record slices as dependencies of other types,
11513 : // because we can fill them in completely here with the final
11514 : // size.
11515 6038 : std::vector<Backend::Btyped_identifier> bfields;
11516 12076 : get_backend_slice_fields(gogo, base->array_type(), true, &bfields);
11517 6038 : if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
11518 0 : this->named_btype_ = gogo->backend()->error_type();
11519 6038 : }
11520 858275 : else if (base->interface_type() != NULL
11521 79813 : && !base->interface_type()->is_empty())
11522 : {
11523 : // We do not record interfaces as dependencies of other types,
11524 : // because we can fill them in completely here with the final
11525 : // size.
11526 74545 : std::vector<Backend::Btyped_identifier> bfields;
11527 149090 : get_backend_interface_fields(gogo, base->interface_type(), true,
11528 : &bfields);
11529 74545 : if (!gogo->backend()->set_placeholder_struct_type(bt, bfields))
11530 0 : this->named_btype_ = gogo->backend()->error_type();
11531 74545 : }
11532 : }
11533 :
11534 : // Get the backend representation for a named type.
11535 :
11536 : Btype*
11537 3499395 : Named_type::do_get_backend(Gogo* gogo)
11538 : {
11539 3499395 : if (this->is_error_)
11540 49 : return gogo->backend()->error_type();
11541 :
11542 3499346 : Btype* bt = this->named_btype_;
11543 :
11544 3499346 : if (!gogo->named_types_are_converted())
11545 : {
11546 : // We have not completed converting named types. NAMED_BTYPE_
11547 : // is a placeholder and we shouldn't do anything further.
11548 2913811 : if (bt != NULL)
11549 : return bt;
11550 :
11551 : // We don't build dependencies for types whose sizes do not
11552 : // change or are not relevant, so we may see them here while
11553 : // converting types.
11554 35799 : this->create_placeholder(gogo);
11555 35799 : bt = this->named_btype_;
11556 35799 : go_assert(bt != NULL);
11557 : return bt;
11558 : }
11559 :
11560 : // We are not converting types. This should only be called if the
11561 : // type has already been converted.
11562 585535 : if (!this->is_converted_)
11563 : {
11564 0 : go_assert(saw_errors());
11565 0 : return gogo->backend()->error_type();
11566 : }
11567 :
11568 585535 : go_assert(bt != NULL);
11569 :
11570 : // Complete the backend representation.
11571 585535 : Type* base = this->type_->base();
11572 585535 : Btype* bt1;
11573 585535 : switch (base->classification())
11574 : {
11575 0 : case TYPE_ERROR:
11576 0 : return gogo->backend()->error_type();
11577 :
11578 : case TYPE_VOID:
11579 : case TYPE_BOOLEAN:
11580 : case TYPE_INTEGER:
11581 : case TYPE_FLOAT:
11582 : case TYPE_COMPLEX:
11583 : case TYPE_STRING:
11584 : case TYPE_NIL:
11585 : case TYPE_MAP:
11586 : case TYPE_CHANNEL:
11587 : return bt;
11588 :
11589 380331 : case TYPE_STRUCT:
11590 380331 : if (!this->seen_in_get_backend_)
11591 : {
11592 349209 : this->seen_in_get_backend_ = true;
11593 698418 : base->struct_type()->finish_backend_fields(gogo);
11594 349209 : this->seen_in_get_backend_ = false;
11595 : }
11596 : return bt;
11597 :
11598 14872 : case TYPE_ARRAY:
11599 14872 : if (!this->seen_in_get_backend_)
11600 : {
11601 14861 : this->seen_in_get_backend_ = true;
11602 29722 : base->array_type()->finish_backend_element(gogo);
11603 14861 : this->seen_in_get_backend_ = false;
11604 : }
11605 : return bt;
11606 :
11607 22427 : case TYPE_INTERFACE:
11608 22427 : if (!this->seen_in_get_backend_)
11609 : {
11610 20298 : this->seen_in_get_backend_ = true;
11611 40596 : base->interface_type()->finish_backend_methods(gogo);
11612 20298 : this->seen_in_get_backend_ = false;
11613 : }
11614 : return bt;
11615 :
11616 1033 : case TYPE_FUNCTION:
11617 : // Don't build a circular data structure. GENERIC can't handle
11618 : // it.
11619 1033 : if (this->seen_in_get_backend_)
11620 35 : return gogo->backend()->circular_pointer_type(bt, true);
11621 998 : this->seen_in_get_backend_ = true;
11622 998 : bt1 = Type::get_named_base_btype(gogo, base);
11623 998 : this->seen_in_get_backend_ = false;
11624 998 : if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
11625 1 : bt = gogo->backend()->error_type();
11626 : return bt;
11627 :
11628 3835 : case TYPE_POINTER:
11629 : // Don't build a circular data structure. GENERIC can't handle
11630 : // it.
11631 3835 : if (this->seen_in_get_backend_)
11632 14 : return gogo->backend()->circular_pointer_type(bt, false);
11633 3821 : this->seen_in_get_backend_ = true;
11634 3821 : bt1 = Type::get_named_base_btype(gogo, base);
11635 3821 : this->seen_in_get_backend_ = false;
11636 3821 : if (!gogo->backend()->set_placeholder_pointer_type(bt, bt1))
11637 1 : bt = gogo->backend()->error_type();
11638 : return bt;
11639 :
11640 0 : default:
11641 0 : case TYPE_SINK:
11642 0 : case TYPE_CALL_MULTIPLE_RESULT:
11643 0 : case TYPE_NAMED:
11644 0 : case TYPE_FORWARD:
11645 0 : go_unreachable();
11646 : }
11647 :
11648 : go_unreachable();
11649 : }
11650 :
11651 : // Build a type descriptor for a named type.
11652 :
11653 : Expression*
11654 71013 : Named_type::do_type_descriptor(Gogo* gogo, Named_type* name)
11655 : {
11656 71013 : if (this->is_error_)
11657 3 : return Expression::make_error(this->location_);
11658 :
11659 : // We shouldn't see unnamed type aliases here. They should have
11660 : // been removed by the call to unalias in Type::type_descriptor_pointer.
11661 : // We can see named type aliases via Type::named_type_descriptor.
11662 71010 : go_assert(name != NULL || !this->is_alias_);
11663 :
11664 : // If NAME is not NULL, then we don't really want the type
11665 : // descriptor for this type; we want the descriptor for the
11666 : // underlying type, giving it the name NAME.
11667 71010 : return this->named_type_descriptor(gogo, this->type_,
11668 71010 : name == NULL ? this : name);
11669 : }
11670 :
11671 : // Add to the reflection string. This is used mostly for the name of
11672 : // the type used in a type descriptor, not for actual reflection
11673 : // strings.
11674 :
11675 : void
11676 495996 : Named_type::do_reflection(Gogo* gogo, std::string* ret) const
11677 : {
11678 495996 : this->append_reflection_type_name(gogo, false, ret);
11679 495996 : }
11680 :
11681 : // Add to the reflection string. For an alias we normally use the
11682 : // real name, but if USE_ALIAS is true we use the alias name itself.
11683 :
11684 : void
11685 495996 : Named_type::append_reflection_type_name(Gogo* gogo, bool use_alias,
11686 : std::string* ret) const
11687 : {
11688 495996 : if (this->is_error_)
11689 : return;
11690 495996 : if (this->is_alias_ && !use_alias)
11691 : {
11692 31219 : if (this->seen_alias_)
11693 : return;
11694 31219 : this->seen_alias_ = true;
11695 31219 : this->append_reflection(this->type_, gogo, ret);
11696 31219 : this->seen_alias_ = false;
11697 31219 : return;
11698 : }
11699 464777 : if (!this->is_builtin())
11700 : {
11701 : // When -fgo-pkgpath or -fgo-prefix is specified, we use it to
11702 : // make a unique reflection string, so that the type
11703 : // canonicalization in the reflect package will work. In order
11704 : // to be compatible with the gc compiler, we put tabs into the
11705 : // package path, so that the reflect methods can discard it.
11706 271858 : const Package* package = this->named_object_->package();
11707 271858 : ret->push_back('\t');
11708 271858 : ret->append(package != NULL
11709 462802 : ? package->pkgpath_symbol()
11710 190944 : : gogo->pkgpath_symbol());
11711 271858 : ret->push_back('\t');
11712 271858 : ret->append(package != NULL
11713 80914 : ? package->package_name()
11714 190944 : : gogo->package_name());
11715 271858 : ret->push_back('.');
11716 : }
11717 464777 : if (this->in_function_ != NULL)
11718 : {
11719 4234 : ret->push_back('\t');
11720 4234 : const Typed_identifier* rcvr =
11721 4234 : this->in_function_->func_value()->type()->receiver();
11722 4234 : if (rcvr != NULL)
11723 : {
11724 932 : Named_type* rcvr_type = rcvr->type()->deref()->named_type();
11725 932 : ret->append(Gogo::unpack_hidden_name(rcvr_type->name()));
11726 466 : ret->push_back('.');
11727 : }
11728 8468 : ret->append(Gogo::unpack_hidden_name(this->in_function_->name()));
11729 4234 : ret->push_back('$');
11730 4234 : if (this->in_function_index_ > 0)
11731 : {
11732 1233 : char buf[30];
11733 1233 : snprintf(buf, sizeof buf, "%u", this->in_function_index_);
11734 1233 : ret->append(buf);
11735 1233 : ret->push_back('$');
11736 : }
11737 4234 : ret->push_back('\t');
11738 : }
11739 929554 : ret->append(Gogo::unpack_hidden_name(this->named_object_->name()));
11740 : }
11741 :
11742 : // Import a named type. This is only used for export format versions
11743 : // before version 3.
11744 :
11745 : void
11746 0 : Named_type::import_named_type(Import* imp, Named_type** ptype)
11747 : {
11748 0 : imp->require_c_string("type ");
11749 0 : Type *type = imp->read_type();
11750 0 : *ptype = type->named_type();
11751 0 : go_assert(*ptype != NULL);
11752 0 : imp->require_semicolon_if_old_version();
11753 0 : imp->require_c_string("\n");
11754 0 : }
11755 :
11756 : // Export the type when it is referenced by another type. In this
11757 : // case Export::export_type will already have issued the name. The
11758 : // output always ends with a newline, since that is convenient if
11759 : // there are methods.
11760 :
11761 : void
11762 76370 : Named_type::do_export(Export* exp) const
11763 : {
11764 76370 : exp->write_type(this->type_);
11765 76370 : exp->write_c_string("\n");
11766 :
11767 : // To save space, we only export the methods directly attached to
11768 : // this type.
11769 76370 : Bindings* methods = this->local_methods_;
11770 76370 : if (methods == NULL)
11771 : return;
11772 :
11773 84123 : for (Bindings::const_definitions_iterator p = methods->begin_definitions();
11774 84123 : p != methods->end_definitions();
11775 42643 : ++p)
11776 : {
11777 42643 : exp->write_c_string(" ");
11778 42643 : (*p)->export_named_object(exp);
11779 : }
11780 :
11781 373739 : for (Bindings::const_declarations_iterator p = methods->begin_declarations();
11782 373739 : p != methods->end_declarations();
11783 332259 : ++p)
11784 : {
11785 332259 : if (p->second->is_function_declaration())
11786 : {
11787 289616 : exp->write_c_string(" ");
11788 289616 : p->second->export_named_object(exp);
11789 : }
11790 : }
11791 : }
11792 :
11793 : // Make a named type.
11794 :
11795 : Named_type*
11796 784552 : Type::make_named_type(Named_object* named_object, Type* type,
11797 : Location location)
11798 : {
11799 784552 : return new Named_type(named_object, type, location);
11800 : }
11801 :
11802 : // Finalize the methods for TYPE. It will be a named type or a struct
11803 : // type. This sets *ALL_METHODS to the list of methods, and builds
11804 : // all required stubs.
11805 :
11806 : void
11807 1470878 : Type::finalize_methods(Gogo* gogo, const Type* type, Location location,
11808 : Methods** all_methods)
11809 : {
11810 1470878 : *all_methods = new Methods();
11811 1470878 : std::vector<const Named_type*> seen;
11812 1470878 : Type::add_methods_for_type(type, NULL, 0, false, false, &seen, *all_methods);
11813 1470878 : if ((*all_methods)->empty())
11814 : {
11815 1158130 : delete *all_methods;
11816 1158130 : *all_methods = NULL;
11817 : }
11818 1470878 : Type::build_stub_methods(gogo, type, *all_methods, location);
11819 2899213 : if (type->is_direct_iface_type() || !type->in_heap())
11820 75493 : Type::build_direct_iface_stub_methods(gogo, type, *all_methods, location);
11821 1470878 : }
11822 :
11823 : // Add the methods for TYPE to *METHODS. FIELD_INDEXES is used to
11824 : // build up the struct field indexes as we go. DEPTH is the depth of
11825 : // the field within TYPE. IS_EMBEDDED_POINTER is true if we are
11826 : // adding these methods for an anonymous field with pointer type.
11827 : // NEEDS_STUB_METHOD is true if we need to use a stub method which
11828 : // calls the real method. TYPES_SEEN is used to avoid infinite
11829 : // recursion.
11830 :
11831 : void
11832 1626333 : Type::add_methods_for_type(const Type* type,
11833 : const Method::Field_indexes* field_indexes,
11834 : unsigned int depth,
11835 : bool is_embedded_pointer,
11836 : bool needs_stub_method,
11837 : std::vector<const Named_type*>* seen,
11838 : Methods* methods)
11839 : {
11840 : // Pointer types may not have methods.
11841 1626333 : if (type->points_to() != NULL)
11842 15982 : return;
11843 :
11844 1610409 : const Named_type* nt = type->named_type();
11845 1610409 : if (nt != NULL)
11846 : {
11847 2103007 : for (std::vector<const Named_type*>::const_iterator p = seen->begin();
11848 2103007 : p != seen->end();
11849 513919 : ++p)
11850 : {
11851 513977 : if (*p == nt)
11852 15982 : return;
11853 : }
11854 :
11855 1589030 : seen->push_back(nt);
11856 :
11857 1589030 : Type::add_local_methods_for_type(nt, field_indexes, depth,
11858 : is_embedded_pointer, needs_stub_method,
11859 : methods);
11860 : }
11861 :
11862 1610351 : Type::add_embedded_methods_for_type(type, field_indexes, depth,
11863 : is_embedded_pointer, needs_stub_method,
11864 : seen, methods);
11865 :
11866 : // If we are called with depth > 0, then we are looking at an
11867 : // anonymous field of a struct. If such a field has interface type,
11868 : // then we need to add the interface methods. We don't want to add
11869 : // them when depth == 0, because we will already handle them
11870 : // following the usual rules for an interface type.
11871 1610351 : if (depth > 0)
11872 155397 : Type::add_interface_methods_for_type(type, field_indexes, depth, methods);
11873 :
11874 1610351 : if (nt != NULL)
11875 1589030 : seen->pop_back();
11876 : }
11877 :
11878 : // Add the local methods for the named type NT to *METHODS. The
11879 : // parameters are as for add_methods_to_type.
11880 :
11881 : void
11882 1589030 : Type::add_local_methods_for_type(const Named_type* nt,
11883 : const Method::Field_indexes* field_indexes,
11884 : unsigned int depth,
11885 : bool is_embedded_pointer,
11886 : bool needs_stub_method,
11887 : Methods* methods)
11888 : {
11889 1589030 : const Bindings* local_methods = nt->local_methods();
11890 1589030 : if (local_methods == NULL)
11891 : return;
11892 :
11893 2393232 : for (Bindings::const_declarations_iterator p =
11894 332523 : local_methods->begin_declarations();
11895 2393232 : p != local_methods->end_declarations();
11896 2060709 : ++p)
11897 : {
11898 2060709 : Named_object* no = p->second;
11899 2060709 : bool is_value_method = (is_embedded_pointer
11900 2060709 : || !Type::method_expects_pointer(no));
11901 2060709 : Method* m = new Named_method(no, field_indexes, depth, is_value_method,
11902 2060709 : (needs_stub_method || depth > 0));
11903 2060709 : if (!methods->insert(no->name(), m))
11904 0 : delete m;
11905 : }
11906 : }
11907 :
11908 : // Add the embedded methods for TYPE to *METHODS. These are the
11909 : // methods attached to anonymous fields. The parameters are as for
11910 : // add_methods_to_type.
11911 :
11912 : void
11913 1610351 : Type::add_embedded_methods_for_type(const Type* type,
11914 : const Method::Field_indexes* field_indexes,
11915 : unsigned int depth,
11916 : bool is_embedded_pointer,
11917 : bool needs_stub_method,
11918 : std::vector<const Named_type*>* seen,
11919 : Methods* methods)
11920 : {
11921 : // Look for anonymous fields in TYPE. TYPE has fields if it is a
11922 : // struct.
11923 1610351 : const Struct_type* st = type->struct_type();
11924 890150 : if (st == NULL)
11925 : return;
11926 :
11927 890150 : const Struct_field_list* fields = st->fields();
11928 890150 : if (fields == NULL)
11929 : return;
11930 :
11931 890150 : unsigned int i = 0;
11932 890150 : for (Struct_field_list::const_iterator pf = fields->begin();
11933 5483806 : pf != fields->end();
11934 4593656 : ++pf, ++i)
11935 : {
11936 4593656 : if (!pf->is_anonymous())
11937 4438201 : continue;
11938 :
11939 155457 : Type* ftype = pf->type();
11940 155457 : bool is_pointer = false;
11941 155457 : if (ftype->points_to() != NULL)
11942 : {
11943 100117 : ftype = ftype->points_to();
11944 100117 : is_pointer = true;
11945 : }
11946 155457 : Named_type* fnt = ftype->named_type();
11947 155457 : if (fnt == NULL)
11948 : {
11949 : // This is an error, but it will be diagnosed elsewhere.
11950 2 : continue;
11951 : }
11952 :
11953 155455 : Method::Field_indexes* sub_field_indexes = new Method::Field_indexes();
11954 155455 : sub_field_indexes->next = field_indexes;
11955 155455 : sub_field_indexes->field_index = i;
11956 :
11957 155455 : Methods tmp_methods;
11958 310910 : Type::add_methods_for_type(fnt, sub_field_indexes, depth + 1,
11959 155455 : (is_embedded_pointer || is_pointer),
11960 : (needs_stub_method
11961 155455 : || is_pointer
11962 155455 : || i > 0),
11963 : seen,
11964 : &tmp_methods);
11965 : // Check if there are promoted methods that conflict with field names and
11966 : // don't add them to the method map.
11967 384378 : for (Methods::const_iterator p = tmp_methods.begin();
11968 384378 : p != tmp_methods.end();
11969 228923 : ++p)
11970 : {
11971 228923 : bool found = false;
11972 1477373 : for (Struct_field_list::const_iterator fp = fields->begin();
11973 1477373 : fp != fields->end();
11974 1248450 : ++fp)
11975 : {
11976 1248452 : if (fp->field_name() == p->first)
11977 : {
11978 : found = true;
11979 : break;
11980 : }
11981 : }
11982 457844 : if (!found &&
11983 228921 : !methods->insert(p->first, p->second))
11984 17688 : delete p->second;
11985 : }
11986 155455 : }
11987 : }
11988 :
11989 : // If TYPE is an interface type, then add its method to *METHODS.
11990 : // This is for interface methods attached to an anonymous field. The
11991 : // parameters are as for add_methods_for_type.
11992 :
11993 : void
11994 155397 : Type::add_interface_methods_for_type(const Type* type,
11995 : const Method::Field_indexes* field_indexes,
11996 : unsigned int depth,
11997 : Methods* methods)
11998 : {
11999 155397 : const Interface_type* it = type->interface_type();
12000 856 : if (it == NULL)
12001 : return;
12002 :
12003 856 : const Typed_identifier_list* imethods = it->methods();
12004 856 : if (imethods == NULL)
12005 : return;
12006 :
12007 2965 : for (Typed_identifier_list::const_iterator pm = imethods->begin();
12008 2965 : pm != imethods->end();
12009 2110 : ++pm)
12010 : {
12011 2110 : Function_type* fntype = pm->type()->function_type();
12012 2110 : if (fntype == NULL)
12013 : {
12014 : // This is an error, but it should be reported elsewhere
12015 : // when we look at the methods for IT.
12016 0 : continue;
12017 : }
12018 2110 : go_assert(!fntype->is_method());
12019 2110 : fntype = fntype->copy_with_receiver(const_cast<Type*>(type));
12020 2110 : Method* m = new Interface_method(pm->name(), pm->location(), fntype,
12021 2110 : field_indexes, depth);
12022 2110 : if (!methods->insert(pm->name(), m))
12023 0 : delete m;
12024 : }
12025 : }
12026 :
12027 : // Build stub methods for TYPE as needed. METHODS is the set of
12028 : // methods for the type. A stub method may be needed when a type
12029 : // inherits a method from an anonymous field. When we need the
12030 : // address of the method, as in a type descriptor, we need to build a
12031 : // little stub which does the required field dereferences and jumps to
12032 : // the real method. LOCATION is the location of the type definition.
12033 :
12034 : void
12035 1470878 : Type::build_stub_methods(Gogo* gogo, const Type* type, const Methods* methods,
12036 : Location location)
12037 : {
12038 1470878 : if (methods == NULL)
12039 : return;
12040 2357875 : for (Methods::const_iterator p = methods->begin();
12041 2357875 : p != methods->end();
12042 2045127 : ++p)
12043 : {
12044 2045127 : Method* m = p->second;
12045 2045127 : if (m->is_ambiguous() || !m->needs_stub_method())
12046 1838733 : continue;
12047 :
12048 206394 : const std::string& name(p->first);
12049 :
12050 : // Build a stub method.
12051 :
12052 206394 : const Function_type* fntype = m->type();
12053 :
12054 206394 : static unsigned int counter;
12055 206394 : char buf[100];
12056 206394 : snprintf(buf, sizeof buf, "$this%u", counter);
12057 206394 : ++counter;
12058 :
12059 206394 : Type* receiver_type = const_cast<Type*>(type);
12060 206394 : if (!m->is_value_method())
12061 160994 : receiver_type = Type::make_pointer_type(receiver_type);
12062 206394 : Location receiver_location = m->receiver_location();
12063 206394 : Typed_identifier* receiver = new Typed_identifier(buf, receiver_type,
12064 412788 : receiver_location);
12065 :
12066 206394 : const Typed_identifier_list* fnparams = fntype->parameters();
12067 206394 : Typed_identifier_list* stub_params;
12068 206394 : if (fnparams == NULL || fnparams->empty())
12069 : stub_params = NULL;
12070 : else
12071 : {
12072 : // We give each stub parameter a unique name.
12073 108637 : stub_params = new Typed_identifier_list();
12074 108637 : for (Typed_identifier_list::const_iterator pp = fnparams->begin();
12075 262733 : pp != fnparams->end();
12076 154096 : ++pp)
12077 : {
12078 154096 : char pbuf[100];
12079 154096 : snprintf(pbuf, sizeof pbuf, "$p%u", counter);
12080 308192 : stub_params->push_back(Typed_identifier(pbuf, pp->type(),
12081 308192 : pp->location()));
12082 154096 : ++counter;
12083 : }
12084 : }
12085 :
12086 206394 : const Typed_identifier_list* fnresults = fntype->results();
12087 206394 : Typed_identifier_list* stub_results;
12088 206394 : if (fnresults == NULL || fnresults->empty())
12089 : stub_results = NULL;
12090 : else
12091 : {
12092 : // We create the result parameters without any names, since
12093 : // we won't refer to them.
12094 111641 : stub_results = new Typed_identifier_list();
12095 111641 : for (Typed_identifier_list::const_iterator pr = fnresults->begin();
12096 248842 : pr != fnresults->end();
12097 137201 : ++pr)
12098 274402 : stub_results->push_back(Typed_identifier("", pr->type(),
12099 274402 : pr->location()));
12100 : }
12101 :
12102 206394 : Function_type* stub_type = Type::make_function_type(receiver,
12103 : stub_params,
12104 : stub_results,
12105 : fntype->location());
12106 206394 : if (fntype->is_varargs())
12107 26933 : stub_type->set_is_varargs();
12108 :
12109 : // We only create the function in the package which creates the
12110 : // type.
12111 206394 : const Package* package;
12112 206394 : if (type->named_type() == NULL)
12113 : package = NULL;
12114 : else
12115 196336 : package = type->named_type()->named_object()->package();
12116 206394 : std::string stub_name = gogo->stub_method_name(package, name);
12117 206394 : Named_object* stub;
12118 206394 : if (package != NULL)
12119 184793 : stub = Named_object::make_function_declaration(stub_name, package,
12120 : stub_type, location);
12121 : else
12122 : {
12123 21601 : stub = gogo->start_function(stub_name, stub_type, false,
12124 : fntype->location());
12125 21601 : Type::build_one_stub_method(gogo, m, stub, buf, receiver_type,
12126 21601 : stub_params, fntype->is_varargs(),
12127 : stub_results, location);
12128 21601 : gogo->finish_function(fntype->location());
12129 :
12130 21601 : if (type->named_type() == NULL && stub->is_function())
12131 10058 : stub->func_value()->set_is_unnamed_type_stub_method();
12132 21601 : if (m->nointerface() && stub->is_function())
12133 1 : stub->func_value()->set_nointerface();
12134 : }
12135 :
12136 206394 : m->set_stub_object(stub);
12137 206394 : }
12138 : }
12139 :
12140 : // Build a stub method which adjusts the receiver as required to call
12141 : // METHOD. RECEIVER_NAME is the name we used for the receiver.
12142 : // PARAMS is the list of function parameters.
12143 :
12144 : void
12145 21601 : Type::build_one_stub_method(Gogo* gogo, Method* method,
12146 : Named_object* stub,
12147 : const char* receiver_name,
12148 : const Type* receiver_type,
12149 : const Typed_identifier_list* params,
12150 : bool is_varargs,
12151 : const Typed_identifier_list* results,
12152 : Location location)
12153 : {
12154 21601 : Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
12155 21601 : go_assert(receiver_object != NULL);
12156 :
12157 21601 : Expression* expr = Expression::make_var_reference(receiver_object, location);
12158 21601 : const Type* expr_type = receiver_type;
12159 21601 : expr = Type::apply_field_indexes(expr, method->field_indexes(), location,
12160 : &expr_type);
12161 21601 : if (expr_type->points_to() == NULL)
12162 18668 : expr = Expression::make_unary(OPERATOR_AND, expr, location);
12163 :
12164 21601 : Expression_list* arguments;
12165 21601 : if (params == NULL || params->empty())
12166 : arguments = NULL;
12167 : else
12168 : {
12169 9885 : arguments = new Expression_list();
12170 9885 : for (Typed_identifier_list::const_iterator p = params->begin();
12171 21429 : p != params->end();
12172 11544 : ++p)
12173 : {
12174 11544 : Named_object* param = gogo->lookup(p->name(), NULL);
12175 11544 : go_assert(param != NULL);
12176 11544 : Expression* param_ref = Expression::make_var_reference(param,
12177 : location);
12178 11544 : arguments->push_back(param_ref);
12179 : }
12180 : }
12181 :
12182 21601 : Expression* func = method->bind_method(expr, location);
12183 21601 : go_assert(func != NULL);
12184 21601 : Call_expression* call = Expression::make_call(func, arguments, is_varargs,
12185 : location);
12186 21601 : Type::add_return_from_results(gogo, stub, call, results, location);
12187 21601 : }
12188 :
12189 : // Build direct interface stub methods for TYPE as needed. METHODS
12190 : // is the set of methods for the type. LOCATION is the location of
12191 : // the type definition.
12192 : //
12193 : // This is for an interface holding a pointer to the type and invoking
12194 : // a value method. The interface data is the pointer, and is passed
12195 : // to the stub, which dereferences it and passes to the actual method.
12196 :
12197 : void
12198 75493 : Type::build_direct_iface_stub_methods(Gogo* gogo, const Type* type,
12199 : Methods* methods, Location loc)
12200 : {
12201 75493 : if (methods == NULL)
12202 : return;
12203 :
12204 26729 : bool is_direct_iface = type->is_direct_iface_type();
12205 26729 : bool in_heap = type->in_heap();
12206 203422 : for (Methods::const_iterator p = methods->begin();
12207 203422 : p != methods->end();
12208 176693 : ++p)
12209 : {
12210 176693 : Method* m = p->second;
12211 :
12212 : // We need a direct-iface stub for a value method for a
12213 : // direct-iface type, and for a pointer method for a not-in-heap
12214 : // type.
12215 176693 : bool need_stub = false;
12216 176693 : if (is_direct_iface && m->is_value_method())
12217 : need_stub = true;
12218 176693 : if (!in_heap && !m->is_value_method())
12219 : need_stub = true;
12220 176693 : if (!need_stub || m->is_ambiguous())
12221 80116 : continue;
12222 :
12223 96577 : Type* receiver_type = const_cast<Type*>(type);
12224 96577 : receiver_type = Type::make_pointer_type(receiver_type);
12225 96577 : const std::string& name(p->first);
12226 96577 : Function_type* fntype = m->type();
12227 :
12228 96577 : static unsigned int counter;
12229 96577 : char buf[100];
12230 96577 : snprintf(buf, sizeof buf, "$ptr%u", counter);
12231 96577 : ++counter;
12232 96577 : Typed_identifier* receiver =
12233 193154 : new Typed_identifier(buf, receiver_type, m->receiver_location());
12234 :
12235 96577 : const Typed_identifier_list* params = fntype->parameters();
12236 96577 : Typed_identifier_list* stub_params;
12237 96577 : if (params == NULL || params->empty())
12238 : stub_params = NULL;
12239 : else
12240 : {
12241 : // We give each stub parameter a unique name.
12242 53485 : stub_params = new Typed_identifier_list();
12243 53485 : for (Typed_identifier_list::const_iterator pp = params->begin();
12244 114659 : pp != params->end();
12245 61174 : ++pp)
12246 : {
12247 61174 : char pbuf[100];
12248 61174 : snprintf(pbuf, sizeof pbuf, "$p%u", counter);
12249 122348 : stub_params->push_back(Typed_identifier(pbuf, pp->type(),
12250 122348 : pp->location()));
12251 61174 : ++counter;
12252 : }
12253 : }
12254 :
12255 96577 : const Typed_identifier_list* fnresults = fntype->results();
12256 96577 : Typed_identifier_list* stub_results;
12257 96577 : if (fnresults == NULL || fnresults->empty())
12258 : stub_results = NULL;
12259 : else
12260 : {
12261 : // We create the result parameters without any names, since
12262 : // we won't refer to them.
12263 52323 : stub_results = new Typed_identifier_list();
12264 52323 : for (Typed_identifier_list::const_iterator pr = fnresults->begin();
12265 118139 : pr != fnresults->end();
12266 65816 : ++pr)
12267 131632 : stub_results->push_back(Typed_identifier("", pr->type(),
12268 131632 : pr->location()));
12269 : }
12270 :
12271 96577 : Function_type* stub_type = Type::make_function_type(receiver,
12272 : stub_params,
12273 : stub_results,
12274 : fntype->location());
12275 96577 : if (fntype->is_varargs())
12276 16 : stub_type->set_is_varargs();
12277 :
12278 : // We only create the function in the package which creates the
12279 : // type.
12280 96577 : const Package* package;
12281 96577 : if (type->named_type() == NULL)
12282 : package = NULL;
12283 : else
12284 96571 : package = type->named_type()->named_object()->package();
12285 :
12286 193154 : std::string stub_name = gogo->stub_method_name(package, name) + "2";
12287 96577 : Named_object* stub;
12288 96577 : if (package != NULL)
12289 93916 : stub = Named_object::make_function_declaration(stub_name, package,
12290 : stub_type, loc);
12291 : else
12292 : {
12293 2661 : stub = gogo->start_function(stub_name, stub_type, false,
12294 : fntype->location());
12295 2661 : Type::build_one_iface_stub_method(gogo, m, stub, buf, stub_params,
12296 2661 : fntype->is_varargs(), stub_results,
12297 : loc);
12298 2661 : gogo->finish_function(fntype->location());
12299 :
12300 2661 : if (type->named_type() == NULL && stub->is_function())
12301 6 : stub->func_value()->set_is_unnamed_type_stub_method();
12302 2661 : if (m->nointerface() && stub->is_function())
12303 0 : stub->func_value()->set_nointerface();
12304 : }
12305 :
12306 96577 : m->set_iface_stub_object(stub);
12307 96577 : }
12308 : }
12309 :
12310 : // Build a stub method for METHOD of direct interface type T.
12311 : // RECEIVER_NAME is the name we used for the receiver.
12312 : // PARAMS is the list of function parameters.
12313 : //
12314 : // The stub looks like
12315 : //
12316 : // func ($ptr *T, PARAMS) {
12317 : // (*$ptr).METHOD(PARAMS)
12318 : // }
12319 :
12320 : void
12321 2661 : Type::build_one_iface_stub_method(Gogo* gogo, Method* method,
12322 : Named_object* stub,
12323 : const char* receiver_name,
12324 : const Typed_identifier_list* params,
12325 : bool is_varargs,
12326 : const Typed_identifier_list* results,
12327 : Location loc)
12328 : {
12329 2661 : Named_object* receiver_object = gogo->lookup(receiver_name, NULL);
12330 2661 : go_assert(receiver_object != NULL);
12331 :
12332 2661 : Expression* expr = Expression::make_var_reference(receiver_object, loc);
12333 2661 : expr = Expression::make_dereference(expr,
12334 : Expression::NIL_CHECK_DEFAULT,
12335 : loc);
12336 :
12337 2661 : Expression_list* arguments;
12338 2661 : if (params == NULL || params->empty())
12339 : arguments = NULL;
12340 : else
12341 : {
12342 1819 : arguments = new Expression_list();
12343 1819 : for (Typed_identifier_list::const_iterator p = params->begin();
12344 4550 : p != params->end();
12345 2731 : ++p)
12346 : {
12347 2731 : Named_object* param = gogo->lookup(p->name(), NULL);
12348 2731 : go_assert(param != NULL);
12349 2731 : Expression* param_ref = Expression::make_var_reference(param,
12350 : loc);
12351 2731 : arguments->push_back(param_ref);
12352 : }
12353 : }
12354 :
12355 2661 : Expression* func = method->bind_method(expr, loc);
12356 2661 : go_assert(func != NULL);
12357 2661 : Call_expression* call = Expression::make_call(func, arguments, is_varargs,
12358 : loc);
12359 2661 : Type::add_return_from_results(gogo, stub, call, results, loc);
12360 2661 : }
12361 :
12362 : // Build and add a return statement from a call expression and a list
12363 : // of result parameters. All we need to know is the number of
12364 : // results.
12365 :
12366 : void
12367 24262 : Type::add_return_from_results(Gogo* gogo, Named_object* stub,
12368 : Call_expression* call,
12369 : const Typed_identifier_list* results,
12370 : Location loc)
12371 : {
12372 24262 : Statement* s;
12373 24262 : if (results == NULL || results->empty())
12374 7528 : s = Statement::make_statement(call, true);
12375 : else
12376 : {
12377 16734 : Expression_list* vals = new Expression_list();
12378 16734 : size_t rc = results->size();
12379 16734 : if (rc == 1)
12380 13785 : vals->push_back(call);
12381 : else
12382 : {
12383 9260 : for (size_t i = 0; i < rc; ++i)
12384 6311 : vals->push_back(Expression::make_call_result(call, i));
12385 : }
12386 16734 : s = Statement::make_return_statement(stub, vals, loc);
12387 : }
12388 :
12389 24262 : s->determine_types(gogo);
12390 24262 : gogo->add_statement(s);
12391 24262 : }
12392 :
12393 : // Apply FIELD_INDEXES to EXPR. The field indexes have to be applied
12394 : // in reverse order. *PEXPR_TYPE maintains the type of EXPR; we use
12395 : // this to avoid calling EXPR->type() before the lowering pass.
12396 :
12397 : Expression*
12398 43916 : Type::apply_field_indexes(Expression* expr,
12399 : const Method::Field_indexes* field_indexes,
12400 : Location location,
12401 : const Type** pexpr_type)
12402 : {
12403 43916 : if (field_indexes == NULL)
12404 : return expr;
12405 22315 : expr = Type::apply_field_indexes(expr, field_indexes->next, location,
12406 : pexpr_type);
12407 22315 : const Type* expr_type = *pexpr_type;
12408 44630 : const Struct_type* stype = expr_type->deref()->struct_type();
12409 22315 : go_assert(stype != NULL
12410 : && field_indexes->field_index < stype->field_count());
12411 22315 : if (expr_type->struct_type() == NULL)
12412 : {
12413 33514 : go_assert(expr_type->points_to()->struct_type() == stype);
12414 16757 : expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
12415 : location);
12416 : }
12417 22315 : *pexpr_type = stype->field(field_indexes->field_index)->type();
12418 22315 : return Expression::make_field_reference(expr, field_indexes->field_index,
12419 22315 : location);
12420 : }
12421 :
12422 : // Return whether NO is a method for which the receiver is a pointer.
12423 :
12424 : bool
12425 2032258 : Type::method_expects_pointer(const Named_object* no)
12426 : {
12427 2032258 : const Function_type *fntype;
12428 2032258 : if (no->is_function())
12429 61734 : fntype = no->func_value()->type();
12430 1970524 : else if (no->is_function_declaration())
12431 1970524 : fntype = no->func_declaration_value()->type();
12432 : else
12433 0 : go_unreachable();
12434 2032258 : return fntype->receiver()->type()->points_to() != NULL;
12435 : }
12436 :
12437 : // Given a set of methods for a type, METHODS, return the method NAME,
12438 : // or NULL if there isn't one or if it is ambiguous. If IS_AMBIGUOUS
12439 : // is not NULL, then set *IS_AMBIGUOUS to true if the method exists
12440 : // but is ambiguous (and return NULL).
12441 :
12442 : Method*
12443 4096384 : Type::method_function(const Methods* methods, const std::string& name,
12444 : bool* is_ambiguous)
12445 : {
12446 4096384 : if (is_ambiguous != NULL)
12447 3860913 : *is_ambiguous = false;
12448 4096384 : if (methods == NULL)
12449 : return NULL;
12450 4096356 : Methods::const_iterator p = methods->find(name);
12451 4096356 : if (p == methods->end())
12452 : return NULL;
12453 1604634 : Method* m = p->second;
12454 1604634 : if (m->is_ambiguous())
12455 : {
12456 0 : if (is_ambiguous != NULL)
12457 0 : *is_ambiguous = true;
12458 0 : return NULL;
12459 : }
12460 : return m;
12461 : }
12462 :
12463 : // Return a pointer to the interface method table for TYPE for the
12464 : // interface INTERFACE.
12465 :
12466 : Expression*
12467 182999 : Type::interface_method_table(Type* type,
12468 : Interface_type *interface,
12469 : bool is_pointer,
12470 : Interface_method_tables** method_tables,
12471 : Interface_method_tables** pointer_tables)
12472 : {
12473 182999 : go_assert(!interface->is_empty());
12474 :
12475 182999 : Interface_method_tables** pimt = is_pointer ? method_tables : pointer_tables;
12476 :
12477 182999 : if (*pimt == NULL)
12478 64266 : *pimt = new Interface_method_tables(5);
12479 :
12480 182999 : std::pair<Interface_type*, Expression*> val(interface, NULL);
12481 182999 : std::pair<Interface_method_tables::iterator, bool> ins = (*pimt)->insert(val);
12482 :
12483 182999 : Location loc = Linemap::predeclared_location();
12484 182999 : if (ins.second)
12485 : {
12486 : // This is a new entry in the hash table.
12487 66004 : go_assert(ins.first->second == NULL);
12488 66004 : ins.first->second =
12489 66004 : Expression::make_interface_mtable_ref(interface, type, is_pointer, loc);
12490 : }
12491 182999 : return Expression::make_unary(OPERATOR_AND, ins.first->second, loc);
12492 : }
12493 :
12494 : // Look for field or method NAME for TYPE. Return an Expression for
12495 : // the field or method bound to EXPR. If there is no such field or
12496 : // method, give an appropriate error and return an error expression.
12497 :
12498 : Expression*
12499 700086 : Type::bind_field_or_method(Gogo* gogo, const Type* type, Expression* expr,
12500 : const std::string& name,
12501 : Location location)
12502 : {
12503 1400172 : if (type->deref()->is_error_type())
12504 6 : return Expression::make_error(location);
12505 :
12506 1400160 : const Named_type* nt = type->deref()->named_type();
12507 1400160 : const Struct_type* st = type->deref()->struct_type();
12508 700080 : const Interface_type* it = type->interface_type();
12509 :
12510 : // If this is a pointer to a pointer, then it is possible that the
12511 : // pointed-to type has methods.
12512 700080 : bool dereferenced = false;
12513 700080 : if (nt == NULL
12514 700080 : && st == NULL
12515 104 : && it == NULL
12516 6 : && type->points_to() != NULL
12517 700085 : && type->points_to()->points_to() != NULL)
12518 : {
12519 4 : expr = Expression::make_dereference(expr, Expression::NIL_CHECK_DEFAULT,
12520 : location);
12521 4 : type = type->points_to();
12522 8 : if (type->deref()->is_error_type())
12523 0 : return Expression::make_error(location);
12524 4 : nt = type->points_to()->named_type();
12525 4 : st = type->points_to()->struct_type();
12526 : dereferenced = true;
12527 : }
12528 :
12529 700080 : bool receiver_can_be_pointer = (expr->type()->points_to() != NULL
12530 912475 : || expr->is_addressable());
12531 700080 : std::vector<const Named_type*> seen;
12532 700080 : bool is_method = false;
12533 700080 : bool found_pointer_method = false;
12534 700080 : std::string ambig1;
12535 700080 : std::string ambig2;
12536 700080 : if (Type::find_field_or_method(type, name, receiver_can_be_pointer,
12537 : &seen, NULL, &is_method,
12538 : &found_pointer_method, &ambig1, &ambig2))
12539 : {
12540 700030 : Expression* ret;
12541 700030 : if (!is_method)
12542 : {
12543 433752 : go_assert(st != NULL);
12544 433752 : if (type->struct_type() == NULL)
12545 : {
12546 325797 : if (dereferenced)
12547 : {
12548 2 : go_error_at(location, "pointer type has no field %qs",
12549 2 : Gogo::message_name(name).c_str());
12550 2 : return Expression::make_error(location);
12551 : }
12552 325795 : go_assert(type->points_to() != NULL);
12553 325795 : expr = Expression::make_dereference(expr,
12554 : Expression::NIL_CHECK_DEFAULT,
12555 : location);
12556 651590 : go_assert(expr->type()->struct_type() == st);
12557 : }
12558 433750 : ret = st->field_reference(expr, name, location);
12559 433750 : if (ret == NULL)
12560 : {
12561 0 : go_error_at(location, "type has no field %qs",
12562 0 : Gogo::message_name(name).c_str());
12563 0 : return Expression::make_error(location);
12564 : }
12565 : }
12566 266278 : else if (it != NULL && it->find_method(name) != NULL)
12567 30807 : ret = Expression::make_interface_field_reference(expr, name,
12568 : location);
12569 : else
12570 : {
12571 235471 : Method* m;
12572 235471 : if (nt != NULL)
12573 234987 : m = nt->method_function(name, NULL);
12574 484 : else if (st != NULL)
12575 484 : m = st->method_function(name, NULL);
12576 : else
12577 0 : go_unreachable();
12578 235471 : go_assert(m != NULL);
12579 235471 : if (dereferenced)
12580 : {
12581 2 : go_error_at(location,
12582 : "calling method %qs requires explicit dereference",
12583 2 : Gogo::message_name(name).c_str());
12584 2 : return Expression::make_error(location);
12585 : }
12586 432364 : if (!m->is_value_method() && expr->type()->points_to() == NULL)
12587 36316 : expr = Expression::make_unary(OPERATOR_AND, expr, location);
12588 235469 : ret = m->bind_method(expr, location);
12589 : }
12590 266276 : go_assert(ret != NULL);
12591 700026 : return ret;
12592 : }
12593 : else
12594 : {
12595 50 : if (Gogo::is_erroneous_name(name))
12596 : {
12597 : // An error was already reported.
12598 : }
12599 49 : else if (!ambig1.empty())
12600 0 : go_error_at(location, "%qs is ambiguous via %qs and %qs",
12601 0 : Gogo::message_name(name).c_str(), ambig1.c_str(),
12602 : ambig2.c_str());
12603 49 : else if (found_pointer_method)
12604 0 : go_error_at(location, "method requires a pointer receiver");
12605 49 : else if (it != NULL && it->is_empty())
12606 1 : go_error_at(location,
12607 : "reference to method %qs in interface with no methods",
12608 2 : Gogo::message_name(name).c_str());
12609 91 : else if (it == NULL && type->deref()->interface_type() != NULL)
12610 14 : go_error_at(location,
12611 : ("reference to method %qs in type that is "
12612 : "pointer to interface, not interface"),
12613 28 : Gogo::message_name(name).c_str());
12614 34 : else if (nt == NULL && st == NULL && it == NULL)
12615 2 : go_error_at(location,
12616 : ("reference to field %qs in object which "
12617 : "has no fields or methods"),
12618 4 : Gogo::message_name(name).c_str());
12619 : else
12620 : {
12621 32 : bool is_unexported;
12622 : // The test for 'a' and 'z' is to handle builtin names,
12623 : // which are not hidden.
12624 32 : if (!Gogo::is_hidden_name(name) && (name[0] < 'a' || name[0] > 'z'))
12625 : is_unexported = false;
12626 : else
12627 : {
12628 20 : std::string unpacked = Gogo::unpack_hidden_name(name);
12629 20 : seen.clear();
12630 20 : is_unexported = Type::is_unexported_field_or_method(gogo, type,
12631 : unpacked,
12632 : &seen);
12633 20 : }
12634 20 : if (is_unexported)
12635 11 : go_error_at(location, "reference to unexported field or method %qs",
12636 22 : Gogo::message_name(name).c_str());
12637 : else
12638 21 : go_error_at(location, "reference to undefined field or method %qs",
12639 42 : Gogo::message_name(name).c_str());
12640 : }
12641 50 : return Expression::make_error(location);
12642 : }
12643 700080 : }
12644 :
12645 : // Look in TYPE for a field or method named NAME, return true if one
12646 : // is found. This looks through embedded anonymous fields and handles
12647 : // ambiguity. If a method is found, sets *IS_METHOD to true;
12648 : // otherwise, if a field is found, set it to false. If
12649 : // RECEIVER_CAN_BE_POINTER is false, then the receiver is a value
12650 : // whose address can not be taken. SEEN is used to avoid infinite
12651 : // recursion on invalid types.
12652 :
12653 : // When returning false, this sets *FOUND_POINTER_METHOD if we found a
12654 : // method we couldn't use because it requires a pointer. LEVEL is
12655 : // used for recursive calls, and can be NULL for a non-recursive call.
12656 : // When this function returns false because it finds that the name is
12657 : // ambiguous, it will store a path to the ambiguous names in *AMBIG1
12658 : // and *AMBIG2. If the name is not found at all, *AMBIG1 and *AMBIG2
12659 : // will be unchanged.
12660 :
12661 : // This function just returns whether or not there is a field or
12662 : // method, and whether it is a field or method. It doesn't build an
12663 : // expression to refer to it. If it is a method, we then look in the
12664 : // list of all methods for the type. If it is a field, the search has
12665 : // to be done again, looking only for fields, and building up the
12666 : // expression as we go.
12667 :
12668 : bool
12669 1253658 : Type::find_field_or_method(const Type* type,
12670 : const std::string& name,
12671 : bool receiver_can_be_pointer,
12672 : std::vector<const Named_type*>* seen,
12673 : int* level,
12674 : bool* is_method,
12675 : bool* found_pointer_method,
12676 : std::string* ambig1,
12677 : std::string* ambig2)
12678 : {
12679 : // Named types can have locally defined methods.
12680 1253658 : const Named_type* nt = type->unalias()->named_type();
12681 1253658 : if (nt == NULL && type->points_to() != NULL)
12682 487243 : nt = type->points_to()->unalias()->named_type();
12683 1253658 : if (nt != NULL)
12684 : {
12685 1229803 : Named_object* no = nt->find_local_method(name);
12686 1229803 : if (no != NULL)
12687 : {
12688 235342 : if (receiver_can_be_pointer || !Type::method_expects_pointer(no))
12689 : {
12690 235342 : *is_method = true;
12691 235342 : return true;
12692 : }
12693 :
12694 : // Record that we have found a pointer method in order to
12695 : // give a better error message if we don't find anything
12696 : // else.
12697 0 : *found_pointer_method = true;
12698 : }
12699 :
12700 4176651 : for (std::vector<const Named_type*>::const_iterator p = seen->begin();
12701 4176651 : p != seen->end();
12702 3182190 : ++p)
12703 : {
12704 3182190 : if (*p == nt)
12705 : {
12706 : // We've already seen this type when searching for methods.
12707 239305 : return false;
12708 : }
12709 : }
12710 : }
12711 :
12712 : // Interface types can have methods.
12713 1018316 : const Interface_type* it = type->interface_type();
12714 33975 : if (it != NULL && it->find_method(name) != NULL)
12715 : {
12716 30945 : *is_method = true;
12717 30945 : return true;
12718 : }
12719 :
12720 : // Struct types can have fields. They can also inherit fields and
12721 : // methods from anonymous fields.
12722 2214047 : const Struct_type* st = type->deref()->struct_type();
12723 748066 : if (st == NULL)
12724 : return false;
12725 748066 : const Struct_field_list* fields = st->fields();
12726 748066 : if (fields == NULL)
12727 : return false;
12728 :
12729 748066 : if (nt != NULL)
12730 724310 : seen->push_back(nt);
12731 :
12732 748066 : int found_level = 0;
12733 748066 : bool found_is_method = false;
12734 748066 : std::string found_ambig1;
12735 748066 : std::string found_ambig2;
12736 748066 : const Struct_field* found_parent = NULL;
12737 3873770 : for (Struct_field_list::const_iterator pf = fields->begin();
12738 3873770 : pf != fields->end();
12739 3125704 : ++pf)
12740 : {
12741 3559736 : if (pf->is_field_name(name))
12742 : {
12743 434032 : *is_method = false;
12744 434032 : if (nt != NULL)
12745 410762 : seen->pop_back();
12746 434032 : return true;
12747 : }
12748 :
12749 3125704 : if (!pf->is_anonymous())
12750 2572126 : continue;
12751 :
12752 1107156 : if (pf->type()->deref()->is_error_type()
12753 1107156 : || pf->type()->deref()->is_undefined())
12754 0 : continue;
12755 :
12756 553578 : Named_type* fnt = pf->type()->named_type();
12757 553578 : if (fnt == NULL)
12758 476042 : fnt = pf->type()->deref()->named_type();
12759 238021 : go_assert(fnt != NULL);
12760 :
12761 : // Methods with pointer receivers on embedded field are
12762 : // inherited by the pointer to struct, and also by the struct
12763 : // type if the field itself is a pointer.
12764 553578 : bool can_be_pointer = (receiver_can_be_pointer
12765 553578 : || pf->type()->points_to() != NULL);
12766 553578 : int sublevel = level == NULL ? 1 : *level + 1;
12767 553578 : bool sub_is_method;
12768 553578 : std::string subambig1;
12769 553578 : std::string subambig2;
12770 553578 : bool subfound = Type::find_field_or_method(fnt,
12771 : name,
12772 : can_be_pointer,
12773 : seen,
12774 : &sublevel,
12775 : &sub_is_method,
12776 : found_pointer_method,
12777 : &subambig1,
12778 : &subambig2);
12779 553578 : if (!subfound)
12780 : {
12781 489078 : if (!subambig1.empty())
12782 : {
12783 : // The name was found via this field, but is ambiguous.
12784 : // if the ambiguity is lower or at the same level as
12785 : // anything else we have already found, then we want to
12786 : // pass the ambiguity back to the caller.
12787 1 : if (found_level == 0 || sublevel <= found_level)
12788 : {
12789 1 : found_ambig1 = (Gogo::message_name(pf->field_name())
12790 1 : + '.' + subambig1);
12791 1 : found_ambig2 = (Gogo::message_name(pf->field_name())
12792 1 : + '.' + subambig2);
12793 1 : found_level = sublevel;
12794 : }
12795 : }
12796 : }
12797 : else
12798 : {
12799 : // The name was found via this field. Use the level to see
12800 : // if we want to use this one, or whether it introduces an
12801 : // ambiguity.
12802 64500 : if (found_level == 0 || sublevel < found_level)
12803 : {
12804 64446 : found_level = sublevel;
12805 64446 : found_is_method = sub_is_method;
12806 64446 : found_ambig1.clear();
12807 64446 : found_ambig2.clear();
12808 64446 : found_parent = &*pf;
12809 : }
12810 54 : else if (sublevel > found_level)
12811 : ;
12812 16 : else if (found_ambig1.empty())
12813 : {
12814 : // We found an ambiguity.
12815 16 : go_assert(found_parent != NULL);
12816 16 : found_ambig1 = Gogo::message_name(found_parent->field_name());
12817 16 : found_ambig2 = Gogo::message_name(pf->field_name());
12818 : }
12819 : else
12820 : {
12821 : // We found an ambiguity, but we already know of one.
12822 : // Just report the earlier one.
12823 : }
12824 : }
12825 553578 : }
12826 :
12827 : // Here if we didn't find anything FOUND_LEVEL is 0. If we found
12828 : // something ambiguous, FOUND_LEVEL is not 0 and FOUND_AMBIG1 and
12829 : // FOUND_AMBIG2 are not empty. If we found the field, FOUND_LEVEL
12830 : // is not 0 and FOUND_AMBIG1 and FOUND_AMBIG2 are empty.
12831 :
12832 314034 : if (nt != NULL)
12833 313548 : seen->pop_back();
12834 :
12835 314034 : if (found_level == 0)
12836 : return false;
12837 64213 : else if (found_is_method
12838 39343 : && type->named_type() != NULL
12839 65562 : && type->points_to() != NULL)
12840 : {
12841 : // If this is a method inherited from a struct field in a named pointer
12842 : // type, it is invalid to automatically dereference the pointer to the
12843 : // struct to find this method.
12844 1 : if (level != NULL)
12845 0 : *level = found_level;
12846 1 : *is_method = true;
12847 1 : return false;
12848 : }
12849 64212 : else if (!found_ambig1.empty())
12850 : {
12851 1 : go_assert(!found_ambig1.empty());
12852 1 : ambig1->assign(found_ambig1);
12853 1 : ambig2->assign(found_ambig2);
12854 1 : if (level != NULL)
12855 1 : *level = found_level;
12856 1 : return false;
12857 : }
12858 : else
12859 : {
12860 64211 : if (level != NULL)
12861 4315 : *level = found_level;
12862 64211 : *is_method = found_is_method;
12863 64211 : return true;
12864 : }
12865 748066 : }
12866 :
12867 : // Return whether NAME is an unexported field or method for TYPE.
12868 :
12869 : bool
12870 22 : Type::is_unexported_field_or_method(Gogo* gogo, const Type* type,
12871 : const std::string& name,
12872 : std::vector<const Named_type*>* seen)
12873 : {
12874 22 : const Named_type* nt = type->named_type();
12875 22 : if (nt == NULL)
12876 4 : nt = type->deref()->named_type();
12877 22 : if (nt != NULL)
12878 : {
12879 22 : if (nt->is_unexported_local_method(gogo, name))
12880 : return true;
12881 :
12882 23 : for (std::vector<const Named_type*>::const_iterator p = seen->begin();
12883 23 : p != seen->end();
12884 2 : ++p)
12885 : {
12886 2 : if (*p == nt)
12887 : {
12888 : // We've already seen this type.
12889 11 : return false;
12890 : }
12891 : }
12892 : }
12893 :
12894 21 : const Interface_type* it = type->interface_type();
12895 3 : if (it != NULL && it->is_unexported_method(gogo, name))
12896 : return true;
12897 :
12898 19 : type = type->deref();
12899 :
12900 19 : const Struct_type* st = type->struct_type();
12901 18 : if (st != NULL && st->is_unexported_local_field(gogo, name))
12902 : return true;
12903 :
12904 11 : if (st == NULL)
12905 : return false;
12906 :
12907 10 : const Struct_field_list* fields = st->fields();
12908 10 : if (fields == NULL)
12909 : return false;
12910 :
12911 10 : if (nt != NULL)
12912 10 : seen->push_back(nt);
12913 :
12914 21 : for (Struct_field_list::const_iterator pf = fields->begin();
12915 21 : pf != fields->end();
12916 11 : ++pf)
12917 : {
12918 11 : if (pf->is_anonymous()
12919 4 : && !pf->type()->deref()->is_error_type()
12920 15 : && !pf->type()->deref()->is_undefined())
12921 : {
12922 2 : Named_type* subtype = pf->type()->named_type();
12923 2 : if (subtype == NULL)
12924 0 : subtype = pf->type()->deref()->named_type();
12925 0 : if (subtype == NULL)
12926 : {
12927 : // This is an error, but it will be diagnosed elsewhere.
12928 0 : continue;
12929 : }
12930 2 : if (Type::is_unexported_field_or_method(gogo, subtype, name, seen))
12931 : {
12932 0 : if (nt != NULL)
12933 0 : seen->pop_back();
12934 0 : return true;
12935 : }
12936 : }
12937 : }
12938 :
12939 10 : if (nt != NULL)
12940 10 : seen->pop_back();
12941 :
12942 : return false;
12943 : }
12944 :
12945 : // Class Forward_declaration.
12946 :
12947 974417 : Forward_declaration_type::Forward_declaration_type(Named_object* named_object)
12948 : : Type(TYPE_FORWARD),
12949 974417 : named_object_(named_object->resolve()), warned_(false)
12950 : {
12951 974417 : go_assert(this->named_object_->is_unknown()
12952 : || this->named_object_->is_type_declaration());
12953 974417 : }
12954 :
12955 : // Return the named object.
12956 :
12957 : Named_object*
12958 438377045 : Forward_declaration_type::named_object()
12959 : {
12960 438377045 : return this->named_object_->resolve();
12961 : }
12962 :
12963 : const Named_object*
12964 1549861722 : Forward_declaration_type::named_object() const
12965 : {
12966 1549861722 : return this->named_object_->resolve();
12967 : }
12968 :
12969 : // Return the name of the forward declared type.
12970 :
12971 : const std::string&
12972 37130 : Forward_declaration_type::name() const
12973 : {
12974 37130 : return this->named_object()->name();
12975 : }
12976 :
12977 : // Warn about a use of a type which has been declared but not defined.
12978 :
12979 : void
12980 137 : Forward_declaration_type::warn() const
12981 : {
12982 137 : Named_object* no = this->named_object_->resolve();
12983 137 : if (no->is_unknown())
12984 : {
12985 : // The name was not defined anywhere.
12986 112 : if (!this->warned_)
12987 : {
12988 7 : go_error_at(this->named_object_->location(),
12989 : "use of undefined type %qs",
12990 7 : no->message_name().c_str());
12991 7 : this->warned_ = true;
12992 : }
12993 : }
12994 25 : else if (no->is_type_declaration())
12995 : {
12996 : // The name was seen as a type, but the type was never defined.
12997 8 : if (no->type_declaration_value()->using_type())
12998 : {
12999 2 : go_error_at(this->named_object_->location(),
13000 : "use of undefined type %qs",
13001 2 : no->message_name().c_str());
13002 2 : this->warned_ = true;
13003 : }
13004 : }
13005 : else
13006 : {
13007 : // The name was defined, but not as a type.
13008 17 : if (!this->warned_)
13009 : {
13010 4 : go_error_at(this->named_object_->location(), "expected type");
13011 4 : this->warned_ = true;
13012 : }
13013 : }
13014 137 : }
13015 :
13016 : // Get the base type of a declaration. This gives an error if the
13017 : // type has not yet been defined.
13018 :
13019 : Type*
13020 219182616 : Forward_declaration_type::real_type()
13021 : {
13022 219182616 : if (this->is_defined())
13023 : {
13024 219182593 : Named_type* nt = this->named_object()->type_value();
13025 219182593 : if (!nt->is_valid())
13026 4 : return Type::make_error_type();
13027 219182589 : return this->named_object()->type_value();
13028 : }
13029 : else
13030 : {
13031 23 : this->warn();
13032 23 : return Type::make_error_type();
13033 : }
13034 : }
13035 :
13036 : const Type*
13037 295614938 : Forward_declaration_type::real_type() const
13038 : {
13039 295614938 : if (this->is_defined())
13040 : {
13041 295614852 : const Named_type* nt = this->named_object()->type_value();
13042 295614852 : if (!nt->is_valid())
13043 375 : return Type::make_error_type();
13044 295614477 : return this->named_object()->type_value();
13045 : }
13046 : else
13047 : {
13048 86 : this->warn();
13049 86 : return Type::make_error_type();
13050 : }
13051 : }
13052 :
13053 : // Return whether the base type is defined.
13054 :
13055 : bool
13056 958593591 : Forward_declaration_type::is_defined() const
13057 : {
13058 958593591 : return this->named_object()->is_type();
13059 : }
13060 :
13061 : // Add a method. This is used when methods are defined before the
13062 : // type.
13063 :
13064 : Named_object*
13065 2538 : Forward_declaration_type::add_method(const std::string& name,
13066 : Function* function)
13067 : {
13068 2538 : Named_object* no = this->named_object();
13069 2538 : if (no->is_unknown())
13070 0 : no->declare_as_type();
13071 2538 : return no->type_declaration_value()->add_method(name, function);
13072 : }
13073 :
13074 : // Add a method declaration. This is used when methods are declared
13075 : // before the type.
13076 :
13077 : Named_object*
13078 0 : Forward_declaration_type::add_method_declaration(const std::string& name,
13079 : Package* package,
13080 : Function_type* type,
13081 : Location location)
13082 : {
13083 0 : Named_object* no = this->named_object();
13084 0 : if (no->is_unknown())
13085 0 : no->declare_as_type();
13086 0 : Type_declaration* td = no->type_declaration_value();
13087 0 : return td->add_method_declaration(name, package, type, location);
13088 : }
13089 :
13090 : // Add an already created object as a method.
13091 :
13092 : void
13093 1 : Forward_declaration_type::add_existing_method(Named_object* nom)
13094 : {
13095 1 : Named_object* no = this->named_object();
13096 1 : if (no->is_unknown())
13097 1 : no->declare_as_type();
13098 1 : no->type_declaration_value()->add_existing_method(nom);
13099 1 : }
13100 :
13101 : // Message name.
13102 :
13103 : void
13104 62337 : Forward_declaration_type::do_message_name(std::string* ret) const
13105 : {
13106 62337 : if (this->is_defined())
13107 62337 : this->append_message_name(this->real_type(), ret);
13108 : else
13109 0 : ret->append(this->named_object_->message_name());
13110 62337 : }
13111 :
13112 : // Traversal.
13113 :
13114 : int
13115 176664231 : Forward_declaration_type::do_traverse(Traverse* traverse)
13116 : {
13117 176664231 : if (this->is_defined()
13118 176664231 : && Type::traverse(this->real_type(), traverse) == TRAVERSE_EXIT)
13119 : return TRAVERSE_EXIT;
13120 : return TRAVERSE_CONTINUE;
13121 : }
13122 :
13123 : // Verify the type.
13124 :
13125 : bool
13126 2665526 : Forward_declaration_type::do_verify(Gogo*)
13127 : {
13128 2665526 : if (!this->is_defined() && !this->is_nil_constant_as_type())
13129 : {
13130 28 : this->warn();
13131 28 : return false;
13132 : }
13133 : return true;
13134 : }
13135 :
13136 : // Get the backend representation for the type.
13137 :
13138 : Btype*
13139 541144 : Forward_declaration_type::do_get_backend(Gogo* gogo)
13140 : {
13141 541144 : if (this->is_defined())
13142 541131 : return Type::get_named_base_btype(gogo, this->real_type());
13143 :
13144 13 : if (this->warned_)
13145 13 : return gogo->backend()->error_type();
13146 :
13147 : // We represent an undefined type as a struct with no fields. That
13148 : // should work fine for the backend, since the same case can arise
13149 : // in C.
13150 0 : std::vector<Backend::Btyped_identifier> fields;
13151 0 : Btype* bt = gogo->backend()->struct_type(fields);
13152 0 : return gogo->backend()->named_type(this->name(), bt,
13153 : this->named_object()->location());
13154 0 : }
13155 :
13156 : // Build a type descriptor for a forwarded type.
13157 :
13158 : Expression*
13159 6437 : Forward_declaration_type::do_type_descriptor(Gogo* gogo, Named_type* name)
13160 : {
13161 6437 : Location ploc = Linemap::predeclared_location();
13162 6437 : if (!this->is_defined())
13163 1 : return Expression::make_error(ploc);
13164 : else
13165 : {
13166 6436 : Type* t = this->real_type();
13167 6436 : if (name != NULL)
13168 6436 : return this->named_type_descriptor(gogo, t, name);
13169 : else
13170 0 : return Expression::make_error(this->named_object_->location());
13171 : }
13172 : }
13173 :
13174 : // The reflection string.
13175 :
13176 : void
13177 176400 : Forward_declaration_type::do_reflection(Gogo* gogo, std::string* ret) const
13178 : {
13179 176400 : this->append_reflection(this->real_type(), gogo, ret);
13180 176400 : }
13181 :
13182 : // Export a forward declaration. This can happen when a defined type
13183 : // refers to a type which is only declared (and is presumably defined
13184 : // in some other file in the same package).
13185 :
13186 : void
13187 0 : Forward_declaration_type::do_export(Export*) const
13188 : {
13189 : // If there is a base type, that should be exported instead of this.
13190 0 : go_assert(!this->is_defined());
13191 :
13192 : // We don't output anything.
13193 0 : }
13194 :
13195 : // Make a forward declaration.
13196 :
13197 : Type*
13198 974417 : Type::make_forward_declaration(Named_object* named_object)
13199 : {
13200 974417 : return new Forward_declaration_type(named_object);
13201 : }
13202 :
13203 : // Class Typed_identifier_list.
13204 :
13205 : // Sort the entries by name.
13206 :
13207 : struct Typed_identifier_list_sort
13208 : {
13209 : public:
13210 : bool
13211 810965 : operator()(const Typed_identifier& t1, const Typed_identifier& t2) const
13212 : {
13213 810965 : return (Gogo::unpack_hidden_name(t1.name())
13214 810965 : < Gogo::unpack_hidden_name(t2.name()));
13215 : }
13216 : };
13217 :
13218 : void
13219 145961 : Typed_identifier_list::sort_by_name()
13220 : {
13221 145961 : std::sort(this->entries_.begin(), this->entries_.end(),
13222 : Typed_identifier_list_sort());
13223 145961 : }
13224 :
13225 : // Traverse types.
13226 :
13227 : int
13228 124779416 : Typed_identifier_list::traverse(Traverse* traverse) const
13229 : {
13230 320071849 : for (Typed_identifier_list::const_iterator p = this->begin();
13231 320071849 : p != this->end();
13232 195292433 : ++p)
13233 : {
13234 195294274 : if (Type::traverse(p->type(), traverse) == TRAVERSE_EXIT)
13235 124779416 : return TRAVERSE_EXIT;
13236 : }
13237 : return TRAVERSE_CONTINUE;
13238 : }
13239 :
13240 : // Copy the list.
13241 :
13242 : Typed_identifier_list*
13243 234693 : Typed_identifier_list::copy() const
13244 : {
13245 234693 : Typed_identifier_list* ret = new Typed_identifier_list();
13246 234693 : for (Typed_identifier_list::const_iterator p = this->begin();
13247 564843 : p != this->end();
13248 330150 : ++p)
13249 660300 : ret->push_back(Typed_identifier(p->name(), p->type(), p->location()));
13250 234693 : return ret;
13251 : }
|